Originally posted by MinorDeity:
It Depends(tm).
Yeah, it does. It depends on lots of things. Here are some suggestions, all of which can be broken whenever you feel like it.
1.
How likely is the exception? Sometimes, you can't know. Sometimes, you can. If it's once per run of the program (
e.g. a EOFException used algorithmically to detect the end of a file) then you may handle it differently than if it's a lightning strike kind of event.
2.
Can I recover from the exception? Is there a way to recover from the exception? You might be able to recover automatically, or perhaps with user assistance. Or, maybe you're SOL (
e.g. the OS failed to create a TCP socket for you).
3.
Handle the exception or ignore? Are you going to do something to handle the exception, or will you just ignore it (possibly logging it to the console or a file)? This may depend on the answers to 1. and 2. above.
4.
Where? I tend to catch exceptions that I'm handling near where they occurred. If I'm going to ignore them, I tend to widen the scope of the try to include as many such exceptions as possible. But not always.
5.
How do multiple exceptions interact? If you've got a try block where
N possible exceptions may be thrown, is it important to know which exception (and where it) occurred? Will you recover from the exceptions differently, or will you handle them all in the same way?
6. Exceptions are a control structure.
How do they interact with other control structures? Be aware that an exception can cause you to jump (or not) out of a loop, or a conditional, or a switch. Do you want to (or not)?
I often stick an empty catch in when I'm initially building something, but I'll usually add a special string in a comment that I can search for when I want to make sure I've actually handled everthing.
Here's an example of bad exception handling that is fresh in my mind. It was a careless bit of code that required an unfortunate amount of time to track down and fix.
This is the main dispatch loop of a thread that is receiving messages on a TCP socket. The intent is to repeatedly read a message on the socket's input stream and process it:
code:<HR>
Code:
while (true) {
try {
int messageType = in.readShort();<P> ...
} catch (IOException e) {
e.printStackTrace(errLog);
}
}
There's a huge block of code where the dots (...) are. If any of it throws an exception, the developer didn't know what to do, so he just decided to print a message to a log file.
The attempt to read a short on the socket blocks until two bytes are available. An attempt to read from a socket which is no longer connected results in an IOException (EOFException, subclass of IOException). When the client (on the other end of the socket) closes the socket, the readShort throws the exception. The exception is caught, logged to the error file, and the loop is reentered. Repeat. OMG, a blocking read just turned into an unintended spin wait! It happens on a "server"; it caused performance to degrade drastically and required a human to have to go find the java processes and kill them. Oh yeah, and huge error log files. Yucky.
One easy solution would have been to widen the scope of the try so that it encloses the while. Then, an IOException would jump out of the loop. This may or may not be desirable. A better solution is to catch EOFException and handle it (by jumping out of the loop), or to inquire of the socket if it's still connected in the IOException handler.
(kg*m*m)/(s*s)
[I just read Midlands post to your
other thread. I would echo his suggestions.]