CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Exceptions

Exceptions handling
Introduction
Java's Mechanism of Exception Handling

Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program.

  • When an exception occurs, an object of a particular exception class is created
  • Java provides a number of exception classes to effectively handle certain common exceptions such as division by zero, invalid input, and file not found
  • Division by zero is an arithmetic error and is handled by the class ArithmeticException
  • When a division by zero exception occurs, the program creates an object of the class ArithmeticException
  • When a Scanner object is used to input data into a program, any invalid input errors are handled using the class InputMismatchException
  • The class Exception (directly or indirectly) is the superclass of all the exception classes in Java

try/catch/finally Block

  • Statements that might generate an exception are placed in a try block
  • The try block might also contain statements that should not be executed if an exception occurs
  • The try block is followed by zero or more catch blocks
  • A catch block specifies the type of exception it can catch and contains an exception handler
  • The last catch block may or may not be followed by a finally block
  • Any code contained in a finally block always executes, regardless of whether an exception occurs, except when the program exits early from a try block by calling the method System.exit
  • If a try block has no catch block, then it must have the finally block
  • If no exception is thrown in a try block, all catch blocks associated with the try block are ignored and program execution resumes after the last catch block
  • If an exception is thrown in a try block, the remaining statements in the try block are ignored
    • The program searches the catch blocks in the order in which they appear after the try block and looks for an appropriate exception handler
  • If the type of the thrown exception matches the parameter type in one of the catch blocks, the code of that catch block executes and the remaining catch blocks after this catch block are ignored
  • If there is a finally block after the last catch block, the finally block executes regardless of whether an exception occurs

Order of catch Blocks

  • The heading of a catch block specifies the type of exception it handles
  • A catch block can catch either all exceptions of a specific type or all types of exceptions
  • A reference variable of a superclass type can point to an object of its subclass
  • If in the heading of a catch block you declare an exception using the class Exception, then that catch block can catch all types of exceptions because the class Exception is the superclass of all exception classes
  • In a sequence of catch blocks following a try block, a catch block declaring an exception of a subclass type should be placed before catch blocks declaring exceptions of a superclass type
Previous | Java's Mechanism of Exception Handling | Exception hierarchy | Exceptions propagation | Next