java - Different way how to throw multiple exceptions -
java - Different way how to throw multiple exceptions -
i confused best practice in situation when need throw multiple exceptions.
let have exceptions declared shown below.
public class daoexception extends exception public class clientnotfoundexception extends daoexception public class notuniqueresultexception extends daoexception
now have а method, can throw clientnotfoundexception
, notuniqueresultexception
. improve appled throws
clause?
getclientbynumber(long number) throws clientnotfoundexception, notuniqueresultexception
or
getclientbynumber(long number) throws daoexception
?
how solve situation in code? creatе own exception hierarchy in applications?
is there other possible way how solve it?
my personal sentiment should descriptive possible possible cases may occur when method activity, i.e. advice do:
getclientbynumber(long number) throws clientnotfoundexception, notuniqueresultexception
the reason 1 invokes method have deal both of cases in method throws exception. method may throw different exception under different circumstances, , way exception caught may differ, depending on type of exception. example:
public void mymethod() { seek { getclientbynumber(1); } grab (notuniqueresultexception e) { //something deals exception of specific type. } grab (clientnotfoundexception e) { //something else deals exception of specific type. } }
java exception exception-handling
Comments
Post a Comment