Exceptions: When Things Go Wrong

Chapter Summary

Technical Terms 

    • catch block 
    • catch an exception 
    • checked exception 
    • dialog box 
    • dynamic scope 
    • error dialog 
    • exception 
    • exception handling 
    • finally block 
    • method call stack 
    • method stack trace 
    • modal dialog 
    • static scope 
    • throw an exception 
    • try block 
    • unchecked exception

The Try/Catch Statement 

The try/catch/finally statement has the following syntax:Annotation 2020-03-29 183212

The try block is meant to include a statement or statements that might throw an exception. The catch blocks—there can be one or more—are meant to handle exceptions that are thrown in the try block. A catch block will handle any exception that matches its parameter class, including subclasses of that class. The finally block is optional. It will be executed whether an exception is thrown or not. If an exception is thrown in the try block, the try block is exited permanently.

The throw statement inside the try block is there to illustrate how throw can be used. You will usually not see a throw statement in a try block, because most throws are done from within Java library methods, which are called from a try block.


Summary of Important Points

  • In Java, when an error or exceptional condition occurs, you throw an Exception, which is caught by special code known as an exception handler. A throw statement—throw new Exception()—is used to throw an exception. 
  • A try block is a block of statements containing one or more statements that may throw an exception. Embedding a statement in a try block indicates your awareness that it might throw an exception and your intention to handle the exception. 
  • Java distinguishes between checked and unchecked exceptions. Checked exceptions must either be caught by the method in which they occur or you must declare that the method containing that statement throws the exception. 
  • Unchecked exceptions are those that belong to subclasses of RuntimeException. If they are left uncaught, they will be handled by Java’s default exception handlers.
  • A catch block is a block of statements that handles the exceptions that match its parameter. A catch block can only follow a try block, and there may be more than one catch block for each try block. 
  • The try/catch syntax allows you to separate the normal parts of an algorithm from special code meant to handle errors and exceptional conditions. 
  • A method stack trace is a trace of the method calls that have led to the execution of a particular statement in the program. The Exception.printStackTrace() method can be called by exception handlers to print a trace of exactly how the program reached the statement that threw the exception. 
  • Static scoping refers to how the text of the program is arranged. If a variable is declared within a method or a block, its static scope is confined to that method or block.
  • Dynamic scoping refers to how the program is executed. A statement is within the dynamic scope of a method or block if it is called from that method or block, or if it is called by some other method that was called from that method or block. 
  • When searching for a catch block to handle an exception thrown by a statement, Java searches upward through the statement’s static scope and backward through its dynamic scope until it finds a matching catch block. If none is found, the Java Virtual Machine will handle the exception itself by printing an error message and a method stack trace. 
  • Many Java library methods throw exceptions when an error occurs. These throw statements do not appear in the program. For example, Java’s integer division operator will throw an ArithmeticException if an attempt is made to divide by zero. 
  • Generally, there are four ways to handle an exception: (1) Let Java handle it; (2) fix the problem that led to the exception and resume the program; (3) report the problem and resume the program; and (4) print an error message and terminate the program. Most erroneous conditions reported by exceptions are difficult or impossible to fix.
  • A finally statement is an optional part of a try/catch block. Statements contained in a finally block will be executed whether an exception is raised or not. 
  • A well-designed program should use exception handling to deal with truly exceptional conditions, not as a means of normal program control. 
  • User-defined exceptions can be defined by extending the Exception class or one of its subclasses.