views:

118

answers:

5

I am using JDK1.6_14. This is a generic question though.

When to go for error handling and when to go for exception handling?

For example, when I carry a division operation out, I can either check if the denominator is zero and throw an error or just handle ArithmeticException.

But when to go for which method?

Thanks.

+1  A: 

Class Throwable has two immediate subclasses: Error and Exception. The subclass of Error are basically used for signaling abnormal system conditions. The subclass of Exception are, in general, recoverable. You can choose to handle the exception by using try-catch-finally.

adatapost
Except for the `RuntimeException` : P.
JG
You can catch `RuntimeExceptions` just like any other old exception.
Bombe
Yes, but I suppose it's not going to be recoverable in almost every situation.
JG
Actually, you cannot (correctly) generalize about what subtypes of Throwable are recoverable. It is very application dependent. In some cases RuntimeExceptions and even some Errors are recoverable. In other cases, even an IOException may not recoverable.
Stephen C
+2  A: 

This can help to understand the difference differences-betweeen-exception-and-error. When you know the difference you are able to handle them. ;)

Markus Lausberg
I think the question was not about Error/Exception but more general on when to throw an Exception or when to handle the error directly where it occurs (e.g. 'in the method')
Andreas_D
It helps to read some articles about the topic you want to start with, when you are not sure how it should look like.
Markus Lausberg
+6  A: 

In general, avoid provoking an exception which you could easily have avoided by execution-time testing and which could be a problem for reasons other than a bug elsewhere in your code.

If you're going to divide by something and you have no reason to trust that the divisor is 0 (e.g. if it was user entered) then check the input first.

If, however, the divisor should never be zero for other reasons (e.g. it's the size of a collection which should definitely be non-empty) then it's at least more reasonable to let that bubble up as an exception. In that case you shouldn't be handling that specific exception though, as you're not expecting it to go wrong. You may want to defensively guard against the possibility anyway - but if you spot an error like that, you're probably going to want to throw some other kind of exception to indicate the bug. Again, this shouldn't be "handled" other than near the top of the stack where you might be catching general exceptions (e.g. so that a server can keep going even if a single request fails).

In general you should only be handling exceptions which you can't reasonably predict - e.g. an I/O failure, or a web service not being present etc.

Jon Skeet
Thanks Jon! Is there any predefined rules or practices on this topic?
bdhar
There are few generally accepted "rules" or "practice". And this can be a bit of a problem if you mix libraries from different sources.
Stephen C
My personal practices: don't catch Errors or Throwables, and don't catch Exceptions unless you either handle them or rethrow them with usefull information for the poor maintainer who will be called in the middle of the night...
extraneon
A: 

Error Handling is the handling of System Generated error,which may evoked by any unbalance system components or by other means,where as,Exception handling is handling of exceptions which may be generate by your program or related software.

you might not know that your denominator is going to be zero in some problem ,which is going through vast calculation.it is good to put all your code in the try/catch block

JavaResp
System pr0n! \o/
Bombe
+3  A: 

There are these cases:

  1. Something can go wrong and you can do something about it (say the user supplied an invalid value in the GUI).

  2. Something can go wrong but there is little you can do (example: some server on the Internet died)

  3. Something can go wrong and the existing exception doesn't carry enough information to find out what happened (think IOException("Can't read file") vs. IOException("Can't read file "+file) vs. IOException("Can't read file "+file.getAbsolutePath())).

Solutions:

  1. Use try-catch to handle the error and take the appropriate action to solve the issue. In the example, show a message somewhere and select the field in the GUI with the illegal value.

  2. Throw the exception up in the hope that the code further up in the stack can do something about it or, as a last resort, present the exception to the user. In the example, ask the user to try again later.

  3. If it would be hard for the user/developer to figure out what happened when seeing the exception message, enrich it. So when File.delete() fails, wrap it in a RuntimeException which says which file couldn't be deleted.

Aaron Digulla
Solution 1: You almost never handle an Error as it generally is not something that **can** be handled. Depending on the Exception it may or may not be possible to handle it (retry a few times on an IOException for instance).
extraneon
Please read carefully. I said "error", not "Error" (as in "java.lang.Error").
Aaron Digulla