6
Handling Exceptions
•To handle an exception, you use a try catch block try
–{
–  (try block statements...)
–}
–catch (ExceptionType ParameterName)
–{
–  (catch block statements...)
–}
•try block contains code that might cause an exception to occur
•catch indicates the exception that you expect to catch and how you want to handle it.
(the curly braces are required).
A try block is:
one or more statements that are executed, and
can potentially throw an exception.
The application will not halt if the try block throws an exception.
After the try block, a catch clause appears
A catch clause begins with the key word catch:
catch (ExceptionType ParameterName)
ExceptionType is the name of an exception class and
ParameterName is a variable name which will reference the exception object if the code in the try block throws an exception.
The code that immediately follows the catch clause is known as a catch block (the curly braces are required).
The code in the catch block is executed if the try block throws an exception.