19.3. Example - Fibonacci Numbers

 
[Page 580 ( continued )]

17.3. Exceptions and Exception Types

A Java exception is an instance of a class derived from Throwable . The Throwable class is contained in the java.lang package, and subclasses of Throwable are contained in various packages. Errors related to GUI components are included in the java.awt package; numeric exceptions are included in the java.lang package because they are related to the java.lang.Number class. You can create your own exception classes by extending Throwable or a subclass of Throwable . Figure 17.3 shows some of Java's predefined exception classes.

Figure 17.3. Exceptions thrown are instances of the classes shown in this diagram, or of subclasses of one of these classes.


[Page 581]

Note

The class names Error , Exception , and RuntimeException are somewhat confusing. All three of these classes are exceptions, and all of the errors discussed here occur at runtime.


The exception classes can be classified into three major types: system errors, exceptions, and runtime exceptions.

  • System errors are thrown by the JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. Examples of subclasses of Error are listed in Table 17.1.

    Table 17.1. Examples of Subclasses of Error
    Class Possible Reason for Exception
    LinkageError A class has some dependency on another class, but the latter class has changed incompatibly after the compilation of the former class.
    VirtualMachineError The JVM is broken or has run out of the resources necessary for it to continue operating.
    AWTError A fatal error in the GUI runtime system.
    AssertionError An assertion has failed. Assertions will be introduced in §17.8, "Assertions."

  • Exceptions are represented in the Exception class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program. Examples of subclasses of Exception are listed in Table 17.2.

    Table 17.2. Examples of Subclasses of Exception
    Class Possible Reason for Exception
    ClassNotFoundException Attempt to use a class that does not exist. This exception would occur, for example, if you tried to run a nonexistent class using the java command, or if your program was composed of, say, three class files, only two of which could be found.
    CloneNotSupported Exception Attempt to clone an object whose defining class does not implement the Cloneable interface. Cloning objects were introduced in Chapter 10, "Abstract Classes and Interfaces."
    IOException Related to input/output operations, such as invalid input, reading past the end of a file, and opening a nonexistent file. Examples of subclasses of IOException are InterruptedIOException , EOFException (EOF is short for End Of File), and FileNotFound Exception .
    AWTException Exceptions in GUI components.

  • Runtime exceptions are represented in the RuntimeException class, which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM. Examples of subclasses are listed in Table 17.3.


    [Page 582]
    Table 17.3. Examples of Subclasses of RuntimeException
    Class Possible Reason for Exception
    ArithmeticException Dividing an integer by zero. Note that floating-point arithmetic does not throw exceptions. See Appendix E, "Special Floating-Point Values."
    NullPointerException Attempt to access an object through a null reference variable.
    IndexOutOfBoundsException Index to an array is out of range.
    IllegalArgumentException A method is passed an argument that is illegal or inappropriate.

RuntimeException , Error , and their subclasses are known as unchecked exceptions . All other exceptions are known as checked exceptions , meaning that the compiler forces the programmer to check and deal with them.

In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it; an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in a program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate that you write code to catch or declare unchecked exceptions.

Caution

At present, Java does not throw integer overflow exceptions. The following statement adds 1 to the maximum integer.

   int   number = Integer.MAX_VALUE +   1   ; System.out.println(number); 

It displays which is logically incorrect. The cause of this problem is overflow, that is, the result exceeds the maximum for an int value.

A future version of Java may fix this problem by throwing an overflow exception.


Tip

For processing large integral values, use the BigInteger class, introduced in §10.5.6, " BigInteger and BigDecimal Classes."


 


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