exp
refers to an object of class Exception
(or a subclass).
try
{
// statements, some of which might throw an exception
}
catch ( SomeExceptionType exp ) // may be omitted if there is a finally block
{
// statements to handle
// SomeExceptionType exceptions
}
// additional catch blocks (optional)
// finally block (optional, unless there are no catch blocks)
When a catch{}
block receives control it
has a reference to an object of class Exception
(or a subclass of Exception
).
The class of the object depends on what exception was thrown.
When an exception event occurs while a program is running,
the Java run time system takes over and
creates an Exception
object to represent the event.
Information about the event is put in the object.
If the exception arose inside a try{}
block,
the Java run time system
sends the Exception
object to the
appropriate catch{}
block
(if there is one).
The parameter of the catch{}
block
(exp
in the above)
refers to
the Exception
object.
Does an Exception
object contain methods, like ordinary objects?