60 likes | 238 Views
An Exception is…. 0. An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is a section of program code that is executed when a particular exception occurs. . Why have exception handlers?. 0.
E N D
An Exception is… 0 An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is a section of program code that is executed when a particular exception occurs.
Why have exception handlers? 0 • With no exception handler for an exception, your program ends at the point of the exception (with no user understandable data from the O/S) • With an exception handler, you can handle the unusual condition in an understandable way
Try/Catch/Throw 0 • Try: The block of code monitored for exceptions • Catch: The block of code following the try block handles an exception type • Throw: When an exception is detected, you throw an exception type
The try-catch Statement 0 How one part of the program catches and processes the exception that another part of the program throws. TryCatchStatement try Block (exception can be thrown within this block or any called functions) catch (FormalParameter1) Block catch (FormalParameter2) FormalParameter DataType VariableName …
Execution of try-catch 0 A statement throws an exception No statements throw an exception Exception Handler Control moves directly to exception handler Statements to deal with exception are executed Statement following entire try-catch statement
O/S (Unix) or you can throw exceptions 0 • O/S thrown examples • No storage available for new request, divide by zero • User thrown exceptions • Using the throw statement