How to handle exceptions in Java Web Applications?-Collection of common programming errors
An exception can be handled in different ways. In this case, it seems that the exception is resulting in a user notification or an error message.
Now, the example you have shown should not throw any exception of that kind that you need to notify user about that. First of all, validate user input and don’t make the flow of your program using exception.
Well, there are exceptions where we need to notify user with some message. That can be done in controller using messages stored in property files, typically.
You must know where to catch exception, where to throw it up, and where to log it. Here first thing I would like to suggest is not to do both, re-throw and log. If you think logging here is appropriate, don’t throw it. If you think throwing is appropriate, then don’t log it. If you follow this way, you will automatically know which one you need to log and which one to throw. That doesn’t mean some exceptions would go away without logging. The method which doesn’t throw that further, holds the responsibility to log that.
Another thing is to know where to use check exception and where runtime. The methods which throw runtime exceptions are somewhat easier to use, per say. But that doesn’t essentially means that you should always use runtime exception. Well, that also depends.
I hope you are getting my point.