1. Output of thrown Exceptions and Errors
Sometimes, your program does something that it shouldn't do, like use too much memory, or divide by 0, or try to read from a non-existing file. These events are called exceptions. When such a thing happens, we say that an "exception is thrown". Later, you'll see why this terminology is used. In Java, these exceptions generally fall into two categories: Errors and Exceptions.
Throwing an
In this Part, we investigate Errors and Exceptions and show how your program can handle exceptions.
Throwing-an-Exception output
When a program is "aborted", or terminated by throwing an exception, Java prints in the Console information about the state of affairs when the exception occurred. Click the icon to the left to see this 5 minute video that explains that output. Here's a pdf file containing the script.
As you saw, termination of a program by throwing an exceptions results in the "call stack" being printed in the Java console. This is a list of the methods that have been called but whose bodies are still being executed, in the reverse order in which they were called. This call stack can be helpful in finding out where and why the exceptions occurred.
Some Errors
Throwing an Error causes immediate termination of a program. There is no chance for the program to handle it in some fashion. Here are examples of Errors that you may see:
- OutOfMemoryError
- InternalError
- UnknownError
In the first case, you have to find our why your program used too much memory. In the other two cases, which should rarely occur, it's difficult to say what to do. Something caused things to become really messed up. Perhaps recompiling all files or rebuilding the application may help.
©This material is from the CD ProgramLive by David Gries and Paul Gries