Sending an email when an Exception is Thrown

Posted by hariubc on Stack Overflow See other posts from Stack Overflow or by hariubc
Published on 2010-05-30T01:45:08Z Indexed on 2010/05/30 1:52 UTC
Read the original article Hit count: 255

Filed under:
|
|

Hi:

I have written a java class where if a method throws an exception, an email is sent, via java mail, with a report to the administrators.

It works - my question is w.r.t elegance - to catch the exception thrown by the main method, the sendEmail() method resides in the catch block of the main method. The sendEmail() method has its own try-catch block.

In effect - it looks like below - is there a more beautiful way of writing this?

try {  
    foo;  
}  
catch {  
   try{  
    sendEmail();  
  }  
  catch {  
   log(e.message);  
  }  
}  

© Stack Overflow or respective owner

Related posts about java

Related posts about exception-handling