Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it OK to use exception handling for business logic?

I think it is accepted that as a general rule in Java (and perhaps any language with exception handling) one should try to avoid using exception handling to actually handle business logic. In general, if it is expected that a certain situation is supposed to happen, one should check for it and handle it more directly than relying on exception handling to do the checking for you. For example, the following is not considered good practice:

try{   _map.put(myKey, myValue); } catch(NullPointerException e){   _map = new HashMap<String, String>(); } 

Instead lazy initialization should be accomplished more like this:

if(_map == null){   _map = new HashMap<String, String>(); } _map.put(myKey, myValue); 

Of course there could be far more complex logic than simply handling lazy initialization. So given that this type of thing is usually frowned upon...when, if ever, is it a good idea to rely on an exception happening for certain business logic to occur? Would it be accurate to say that any instance where one feels compelled to use this approach is really highlighting a weakness of the API being used?

like image 347
Michael McGowan Avatar asked Mar 21 '11 13:03

Michael McGowan


People also ask

When should exception handling be used?

“Exception handling is needed to identify problems that program >cannot handle and tell them about the user, because user can >handle them” Instead of throwing an exception I can return the error value and in the calling function i can check for the error and display a message which will help the user to handle it.

Why is it important to use exception handling?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.

Under what condition should an exception always be thrown?

In short: You should throw an exception if a method is not able to do the task it is supposed to do.

What is exception handling why is it used give an example?

Examples include a user providing abnormal input, a file system error being encountered when trying to read or write a file, or a program attempting to divide by zero. Exception handling attempts to gracefully handle these situations so that a program (or worse, an entire system) does not crash.


1 Answers

Whenever the exception can be anticipated but not avoided.

Say, if you are relying on an external API of some sort to parse data, and that API offers parse methods but nothing to tell whether a given input can be parsed or not (or if whether the parse can succeed or not depends on factors out of your control, but the API doesn't provide appropriate function calls), and the parsing method throws an exception when the input cannot be parsed.

With a properly designed API, this should boil down to a quantity somewhere in the range "virtually never" to "never".

I can see absolutely no reason to use exception handling as a means of normal flow control in code. It's expensive, it's hard to read (just look at your own first example; I realize it was probably written very quickly, but when _map hasn't been initialized, what you end up with is an empty map, throwing away the entry you were trying to add), and it litters the code with largely useless try-catch blocks, which can very well hide real problems. Again taking your own example, what if the call to _map.add() were to throw a NullPointerException for some reason other than _map being null? Suddenly, you are silently recreating an empty map rather than adding an entry to it. Which I'm sure I don't really have to say can lead to any number of bugs in completely unrelated places in the code because of unexpected state...

Edit: Just to be clear, the above answer is written in the context of Java. Other languages may (and apparently, do) differ in the implementation expense of exceptions, but other points should still hold.

like image 124
user Avatar answered Sep 22 '22 22:09

user