Java: catching a general exception, just in case? -


im new java here, guessing while working here.

i had code

    try {         sendmailwithattachment();       } catch (addressexception e) {          e.printstacktrace();         sendmailwithoutattachment();      } catch (messagingexception e) {          e.printstacktrace();         sendmailwithoutattachment();     }  

but "catch" code never executed, suspected there type of exception im not catching, , instead of guessing was, decided catch "general" kind of exception, , blindly tried this

    try {         sendmailwithattachment       } catch (addressexception e) {          e.printstacktrace();         sendmailwithoutattachment();      } catch (messagingexception e) {          e.printstacktrace();         sendmailwithoutattachment();     }  catch (exception e) {         e.printstacktrace();         sendmailwithoutattachment();     } 

and catch code ran.

what have done here, , importantly:

can catch general exception, in case?

all exceptions subclass exception class. so, have done said, "if addressexception happens, handle this; if messagingexception happens, handle this; other exception, handle this."

in case, 3 catch blocks identical, can use catch (exception e) { ... } block, considered poor coding. typically, should know exceptions code might throw , handle each of exceptions appropriately.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -