Java Reference
In-Depth Information
C HAPTER S UMMARY
1.
Exception handling enables a method to throw an exception to its caller.
2.
A Java exception is an instance of a class derived from java.lang.Throwable .
Java provides a number of predefined exception classes, such as Error , Exception ,
RuntimeException , ClassNotFoundException , NullPointerException , and
ArithmeticException . You can also define your own exception class by extending
Exception .
3.
Exceptions occur during the execution of a method. RuntimeException and Error
are unchecked exceptions ; all other exceptions are checked .
4. When declaring a method , you have to declare a checked exception if the method might
throw it, thus telling the compiler what can go wrong.
5.
The keyword for declaring an exception is throws , and the keyword for throwing an
exception is throw .
6.
To invoke the method that declares checked exceptions, enclose it in a try statement.
When an exception occurs during the execution of the method, the catch block catches
and handles the exception.
7.
If an exception is not caught in the current method, it is passed to its caller. The process
is repeated until the exception is caught or passed to the main method.
8.
Various exception classes can be derived from a common superclass. If a catch block
catches the exception objects of a superclass, it can also catch all the exception objects
of the subclasses of that superclass.
9.
The order in which exceptions are specified in a catch block is important. A compile
error will result if you specify an exception object of a class after an exception object of
the superclass of that class.
10. When an exception occurs in a method, the method exits immediately if it does not
catch the exception. If the method is required to perform some task before exiting, you
can catch the exception in the method and then rethrow it to its caller.
11.
The code in the finally block is executed under all circumstances, regardless of whether
an exception occurs in the try block or whether an exception is caught if it occurs.
12.
Exception handling separates error-handling code from normal programming tasks,
thus making programs easier to read and to modify.
13.
Exception handling should not be used to replace simple tests. You should perform
simple test using if statements whenever possible, and reserve exception handling for
dealing with situations that cannot be handled with if statements.
14.
The File class is used to obtain file properties and manipulate files. It does not contain
the methods for creating a file or for reading/writing data from/to a file.
15.
You can use Scanner to read string and primitive data values from a text file and use
PrintWriter to create a file and write data to a text file.
16.
You can read from a file on the Web using the URL class.
 
 
Search WWH ::




Custom Search