aspose file tools*
The moose likes Beginning Java and the fly likes Exception Catch block- Email trigger! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Catch block- Email trigger!" Watch "Exception Catch block- Email trigger!" New topic
Author

Exception Catch block- Email trigger!

resh singh
Greenhorn

Joined: Aug 14, 2006
Posts: 9
Hi,

While working on the older version of code I see that everywhere in the catch block there is an email getting triggered to the admin.

Like:

try{

}catch(Exception e){
logger.debug("Debug message");
mail.send(, , e.getMessage);
}

I feel this unnecessary, however I want to be sure and would like to get your opinions.

Thanks,
Resh
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

You've posted three lines of code with no explanation at all. It's completely impossible for anybody else to tell whether they are necessary or not. It would take a lot more explanation from you before anybody else could make a reasonable judgement about that code.

Remember that "necessary" isn't something managed by the Java compiler. It's a business decision made by the users of the software and the designers of the software. So your question is about that business decision.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

We really cannot discuss or ponder over the Business decisions. At times they are useful and at times not.


Mohamed Sanaulla | My Blog
resh singh
Greenhorn

Joined: Aug 14, 2006
Posts: 9
Thanks All for replying back.

Steve Harney
Greenhorn

Joined: Jun 19, 2010
Posts: 19
resh singh wrote:Hi,

While working on the older version of code I see that everywhere in the catch block there is an email getting triggered to the admin.

Like:

try{

}catch(Exception e){
logger.debug("Debug message");
mail.send(, , e.getMessage);
}

I feel this unnecessary, however I want to be sure and would like to get your opinions.

Thanks,
Resh


It is a business decission but you have asked for an opinion so ill give my opinion.

I think the idea of directly call to a send mail on each exception is a bad idea, particularly if its being done to send error emails to the admin. I think it would be better to log the error properly and if emails where required set up an SMTPAppender in my log4j to capture and send the messages.

I would probably end up with something looks like this.


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14272
    
  21

resh singh wrote:While working on the older version of code I see that everywhere in the catch block there is an email getting triggered to the admin.

I must admit that I once made a system that did something similar. It was a bad idea. The administrator would get thousands, or even tens of thousands of e-mails from the system because there was some small problem that happened on each message that the system was processing. The amount of mail quickly became unmanageable. Ofcourse, not every system is the same, so maybe the circumstances in your system are different.

Most logging systems have different log levels (for example FATAL, ERROR, INFO, DEBUG, TRACE). When designing and implementing your system, it's a good idea to think about how you're going to use those levels. For example, log exceptions with the ERROR level, major entry points with INFO, information that might be useful for the developers with DEBUG etc.

While testing, you can set the log level to DEBUG, so that everything that's interesting for developers is written in the log file. For production, set it to INFO or to ERROR, so that you don't get too much in the log file. Logging also slows down the program, so you probably don't want to log everything in production.

There are tools that you can use to analyze log files and for example filter out all the ERROR messages. In my opinion it is better to use such a tool than to send e-mail to the administrator directly from the application itself - that way you separate the monitoring functionality from the application itself. If you really want the e-mail, you could let the log analyzer tool send the e-mail, instead of the application itself.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
resh singh
Greenhorn

Joined: Aug 14, 2006
Posts: 9
Thank you all for your responses, I really appreciate it.

That's what I was looking for, I wanted to be sure whats a good practice.

It make more sense to use some log analyzer (specifically on production).

Thank You,
Resh
 
wood burning stoves
 
subject: Exception Catch block- Email trigger!