throw Statement

Purpose:

Executed to indicate that an exception has occurred.

Mechanics:

throw new <Throwable>;

or

throw e; // where e is an instance of the Throwable class or one of its subclasses

Example:

throw new Exception();

Usage:

  • <Throwable> must be the Throwable class or one of its subclasses.
  • When a method throws an exception, it should declare the exceptions it may throw in the method declaration using the throws statement.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu