Class 2 |
Intermediate Java 30-IT-397 |
|
Dealing with Errors
-
When an error occurs, your program should do one of two things:
-
Recover from the error and assume a safe state.
-
Allow the user to save work, then terminate the program.
-
You have to think ahead of time of what errors could occur, then plan for
them with exception handling.
-
You'll probably never know where every error may occur; sometimes
you have to add exception handling as the program ages.
-
The types of errors you should consider include:
-
User input errors: Both intentional and unintentional errors can
occur. The more users you have, and the less educated those users
are, the more you need to bulletproof your application. Always assume
the least amount of knowledge on behalf of the user! On the other
hand, the more intellegent your users are and the more secure your program
has to be, you must also spend more time bulletproofing your application!
-
Device errors: Device errors are often unpredicatable. Is
your program smart enough to deal with them?
-
Too many times programmers will see an error, then just say, "Oh yeah,
the FTP connection failed." This is not an answer, the program
should prepare for this! Anyone who's ever been on call (aka: me)
will testify to this at 3:00 AM!
-
An application I use at the bank will often transfer thousands of files
at a time, each with its own FTP connection. It's an all or nothing
deal - if the FTP connection fails, it must wait 30 minutes and start over.
And, in that time, only more files build up in the queue. Even if
you assume that the FTP connection works 99.9% of the time, that still
means that it will fail at least once during the transfer of thousands
of time sensitive files! To solve this problem, I put in an exception
handler with a recursive retry. More on this to come.
-
Physical limitations: Similar to the above. Out of disk
errors, 1 GB of data over a 28.8 modem, etc. You don't want to get
in a situation where a user, intentionally or accidentally, overloads your
system.
-
Code errors: These get smaller in number as you get more experienced,
but they do happen.
-
Remind me to talk about the .00 error in the same program mentioned in
'device errors'.
Throwing Exceptions

Created by: Brandan Jones
December 17, 2001