Sept. 29th Exceptions Problems in a Java program may cause exceptions or errors. Recall from Chapter 2 that an exception is an object taht defines a problem that can usually be fixed. An error is like an exception except that the problem usually cannot be found. Java has a set of exceptions and errors. Four common exceptions are explored later in this section. We can handle an exceptio nin one of three ways. A program can: - not handle the exception at all, - handle the exception where it occurs, or - handle the exception later in the program. Handling exceptions is not required for the AP exam and is beyond the scope of this book. In the rest of this section we discuss the exception-related topics that are required for the exam: understanding common exceptions and throwing exceptions. Exception Messages If a program doees not handle the exception at all, it will crash and produce a message that describes the exception and where it happened. This information can help you track down the cause of a problem. Lety's look at an exception. The program shown in Listing 5.6 throws an ArithmeticException when the program tries to divide by zero. The program crashes and prints out information about the exception. Note that the last println statement in the program never executes because the exception occurs first. The first line of the output tells us which exception was thrown and gives us some information about why it was thrown. The rest of the output is the call stack trace. This tells us where the exeption occured. In this case, there is only one line in the call stack trace, but there could be several, depending on where the exception originated. The first trace line gives us the method, file and line number where the exception happened. The other trace lines tell us the methods that were called just before the method that produced the exception. In this program, there is only one method, and it produced the exception, s0ot therei so nly one line in the trace. Listing 5.6: ************************ public class Zero { public static void main(String[] args) { int numerator = 10; int denominator = 0; System.out.println(numerator/denominator); System.out.println("This text will not be printed."); } } ******************************** The output should be: Exception in thread "main" java.lang.ArithmeticException: / by zero at zero.main(Zero.java:17) Other common exceptions include NullPointerException, ArrayIndexOutOfBoundsException, and ClassCastException. You'll get a NullPointerException if you use a null reference where you need an object reference. For example, suppose name has been declared to be a reference to a String and it has the value null, indicating that it is not referring to any String at this time. An attempt to call a method on name, such as name.length(), will cause a NullPointerException. We will further discuss ArrayIndexOutOfBoundsException and ClassCastException later. Throwing Exceptions We have seen that the Java runtime environment throws exceptions in exceptional circumstances. As programmers, we too can throw exceptioins. An exception is thrown with a throw statement: the reserved word throw followed by an exception object. For example, the following swtatement thorws a NoSuchElementException: throw new NoSuchElementException(); The exception object is created with the new operator right in the throw statement. Exception classes in Java have a default constructor that takes no arguments and a constructor that takes a single String argument. If provided, this String appears in the exception message when the exception occurs. In later chapteres we look at exceptions such as IllegalStateException and NoSuchElementEdxception thrown in the implementation of abstract data types.