java - The exception is thrown - and the code is executed further -


faced situation - in main method, child method called, checks object, , exception thrown in child method (one of objects in list null). code of main method still continues executed! example code:

@transactional public boolean addcompany(list<company> companies, list<address> addresses) throws exception{     checkaddress(addresses);     try{         for(int = 0; < companies.size(); i++){             if(findcompany(companies.get(i).getid()) == null && !isexistscompany(companies.get(i))){                 companies.get(i).setaddress(addresses.get(i));                 this.em.persist(companies.get(i));             }         }     }catch(exception e){         return false;     }     return true; }  public void checkaddress(list<address> addresses) throws exception{     try{         if(addresses == null)             throw new exception(thread.currentthread().getstacktrace()[2].getclassname() + "." + thread.currentthread().getstacktrace()[2].getmethodname() + "." + thread.currentthread().getstacktrace()[1].getmethodname() + ": invalid parameter: list null");         for(address : addresses)             if(a == null)                 throw new exception(thread.currentthread().getstacktrace()[2].getclassname() + "." + thread.currentthread().getstacktrace()[2].getmethodname() + "." + thread.currentthread().getstacktrace()[1].getmethodname() + ": invalid list item: object null");     }catch(exception e){         e.printstacktrace();     } } 

in regard, several questions arose: - why code not stop? - necessary, option, out of situation changing type of checkaddress method void boolean, , in main method process true/false? - how correctly handled on frontend such error - text send exceptions frontend or process code 500 , if so, why generate exception on backend - in development process? how deal competently? advise please. in advance.

try this:

public void checkaddress(list<address> addresses) throws exception{         if(addresses == null)             throw new exception(thread.currentthread().getstacktrace()[2].getclassname() + "." + thread.currentthread().getstacktrace()[2].getmethodname() + "." + thread.currentthread().getstacktrace()[1].getmethodname() + ": invalid parameter: list null");         for(address : addresses)             if(a == null)                 throw new exception(thread.currentthread().getstacktrace()[2].getclassname() + "." + thread.currentthread().getstacktrace()[2].getmethodname() + "." + thread.currentthread().getstacktrace()[1].getmethodname() + ": invalid list item: object null");     }  } 

what stuff threads? crazy code.

don't allow add null instance list in first place.

i might write way:

public void checkaddresses(list<address> addresses) {     if (addresses == null) throw new illegalargumentexception("address list cannot null");     (address : addresses) {         if (a == null) throw new illegalargumentexception("address cannot null");     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -