19.12. Programming Exercises

 
[Page 596]

Chapter Summary

  • When an exception occurs, Java creates an object that contains the information for the exception. You can use the information to handle the exception.

  • 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 .

  • Exceptions occur during the execution of a method. RuntimeException and Error are unchecked exceptions; all other exceptions are checked exceptions.

  • When declaring a method, you have to declare a checked exception if the method might throw that checked exception, thus telling the compiler what can go wrong.

  • The keyword for declaring an exception is throws , and the keyword for throwing an exception is throw .

  • To invoke the method that declares checked exceptions, you must enclose the method call in a try statement. When an exception occurs during the execution of the method, the catch block catches and handles the exception.

  • 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.

  • If an exception of a subclass of Exception occurs in a GUI component, Java prints the error message on the console, but the program goes back to its user -interface -processing loop to run continuously. The exception is ignored.

  • 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.

  • The order in which exceptions are specified in a catch block is important. A compilation error will result if you do not specify an exception object of a class before an exception object of the superclass of that class.

  • 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 the real handler.

  • The code in the finally block is executed under all circumstances, regardless of whether an exception occurs in the try block or is caught.

  • Exception handling separates error-handling code from normal programming tasks , thus making programs easier to read and to modify.

  • Exception handling should not be used to replace simple tests. You should test simple exceptions whenever possible, and reserve exception handling for dealing with situations that cannot be handled with if statements.


    [Page 597]
  • Exceptions address robustness, whereas assertions address correctness. Exceptions and assertions are not meant to substitute for simple tests. Avoid using exception handling if a simple if statement is sufficient. Never use assertions to check normal conditions.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net