Java Exception Handling - is this good practice? -


i working on java plugin framework.

i have written code in such way entrypoint function looks below (consider starting point, main function)

function entrypoint() {    try{       //some code block       subfunction1();       subfunction2();    }    catch(exception e) {}    catch(ioexception ioe) {}    catch(nullpointerexception npe){} }  function subfunction1() throws ioexception {     //some code }  function subfunction2() throws nullpointerexception {     //some code } 

so idea is, sub functions throws specific exceptions major function , catch these exceptions in major functions , handling.

is way correct? if not please suggest better way.

  • the order of catch statements should changed. since first catch match exceptions, following 2 never triggered.

  • an npe in cases unexpected , not recoverable. catching implies application able recover , run regardless. case?

  • even if npe recoverable, better practice check != null instead of relying on exceptions command flow. conceptual reasons (exception-based command flow requires more code, less readable, intention unclear) performance reasons.

  • all exceptions swallowed - no logging or rethrowing happens. way, no 1 know if , when goes wrong because there no exceptions logged. in cases, users, other developers , maintainers expect exceptions exceptional , therefore logged.


Comments