java - order in multi-catch exception handler -
i know since java 7 can use multi-catch wonder if order of exceptions in matters in previous versions of java? e.g put in exception , sqlexception , ioexception ?
try { // execute code may throw 1 of 3 exceptions below. } catch(exception | sqlexception | ioexception e) { logger.log(e); } or should way ?
try { // execute code may throw 1 of 3 exceptions below. } catch(sqlexception | ioexception e) { logger.log(e); } catch(exception e) { logger.severe(e); }
there's no point in single catch block catch(exception | sqlexception | ioexception e) since exception covers sub-classes ioexception , sqlexception.
therefore catch(exception e) enough if wish same handling of exception types.
if want different handling more general exception, second code snippet makes sense, , here order of 2 catch blocks matters, since must catch more specific exception types first.
Comments
Post a Comment