Java Reference
In-Depth Information
Chapter 10
Exception Handling
OBJECTIVES
• 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.
INTRODUCTION
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.
10.1
Output of thrown Exceptions
Method main of the class shown below tries to print the value of 5/0 :
Activity
10-1.1
public class Ex {
public static void main(String[] args) {
System.out.println(5 / 0);
}
}
Search WWH ::




Custom Search