You are on page 1of 9

Exception Handling

P. Vasuki
Introduction
• Exception is a response to an
exceptional circumstance that arises
while a program is running, such as
an attempt to divide by zero.
• Exceptions provide a way to
transfer control from one part of a
program to another.
Introduction
• Exceptions
– Indicate problems that occur during a
program’s execution
– Occur infrequently
• Exception handling
– Can resolve exceptions
• Allow a program to continue executing or
• Notify the user of the problem and
• Terminate the program in a controlled
manner
Introduction
• Exception Handling
– In real time handle the unexpected errors/
Ubnormal Situations
– Helps to make clear, robust, error tolerant
program
• Some sample - Common failures
– new not allocating memory
– Out of bounds array subscript
– Division by zero
– Invalid function parameters
Other Error-Handling Techniques
• Use assert
– If assertion false, the program terminates
• Set some error indicator
– Program may not check indicator at all points
there error could occur
• Test for the error condition
– Issue an error message and call exit
– Pass error code to environment
Exception handling machanism
• A function can throw an exception object
if it detects an error
– Object typically a character string (error message)
or class object
– If exception handler exists, exception caught and
handled
– Otherwise, program terminates
TRY - THROW - CATCH

method1() { declare e
method2() throws Exception {
try {
invoke method2; if (an error occurs) {
}
catch exception catch (Exception ex) { throw new Exception(); throw exceptio
Process exception; }
} }
}
Format – Exception handling
 Enclose code that may have an error in try block
 Follow with one or more catch blocks
 Each catch block has an exception handler
 If exception occurs and matches parameter in
catch block, code in catch block executed
 If no exception thrown, exception handlers skipped
and control resumes after catch blocks
 throw point - place where exception occurred
 Control cannot return to throw point
Further on Class
• Rethrow
• Multiple Catch
• Default Handler
• Exception Handling

You might also like