next up previous
Next: Example Up: Unit 10 Previous: Definition of new exceptions

The throw statement

To throw an exception, we use the throw statement.


Throwing an exception

Syntax:
throw exceptionObject;

Semantics:

Throws an exception of the type indicated by the object appearing as parameter to the throw statement.

Example:

throw new MyException("message for MyException");

This statement throws an exception of type MyException, which prints on the screen the message that appears as parameter to the constructor.



next up previous
Next: Example Up: Unit 10 Previous: Definition of new exceptions