Java Reference
In-Depth Information
Chapter 10
Exception Handling
• Learn about Java's Exceptional error messages.
• Learn about throwable objects and how they are thrown and caught.
• See how you can create your own throwable objects.
When an error occurs in your program, an exception is thrown . This may lead to
abortion of program execution. In the first section of this chapter, we study the
error messages that are printed when abortion occurs.
We then discuss error handling in Java. Basically, if something untoward
happens, an object of a throwable class is thrown, and another part of the program
can catch it and handle it. The try-statement gives you the ability to catch thrown
objects and handle them. Or, you can simply let another part of the program —
the part that called the method in which the object was thrown— handle them.
The Java exception-handling mechanism allows you to isolate the problems
of handling errors from the part of the program that does calculation in a normal
fashion. Used properly, the exception-handling mechanism is a useful tool.
Output of thrown Exceptions
Method main of the class shown below tries to print the value of 5/0 :
public class Ex {
public static void main(String[] args) {
System.out.println(5 / 0);
Search WWH ::

Custom Search