This week's book giveaway is in the Other Open Source APIs forum. We're giving away four copies of Storm Applied and have Sean Allen, Peter Pathirana & Matthew Jankowski on-line! See this thread for details.
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.
I feel this unnecessary, however I want to be sure and would like to get your opinions.
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.
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.