Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Catch block- Email trigger!

 
resh singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We really cannot discuss or ponder over the Business decisions. At times they are useful and at times not.
 
resh singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All for replying back.

 
Steve Harney
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15437
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
resh singh
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic