Download presentation
Presentation is loading. Please wait.
1
Exception Handling In Text: Chapter 14
2
Chapter 3: Syntax and Semantics
Exception Handling A language feature that allows the programmer to handle runtime exceptional conditions What is an "exceptional condition"? Hardware error Failure in underlying software Any anomalous event It need not be erroneous—just something that requires special handling Chapter 3: Syntax and Semantics
3
Chapter 3: Syntax and Semantics
Examples What are examples of exceptional conditions? Chapter 3: Syntax and Semantics
4
Chapter 3: Syntax and Semantics
Terminology An exception is raised, or signaled, when its associated condition occurs The code that is executed after an exception is raised is called the exception handler This code processes the exception Chapter 3: Syntax and Semantics
5
Chapter 3: Syntax and Semantics
Issues Form of handler: Complete program unit, or code segment? Location of handler: Are exceptions handled in unit where raised, in calling unit, or elsewhere? Binding of handlers to exceptions: Static or dynamic? Transfer of control after exception is handled: Allow unit that raised exception to continue executing? Chapter 3: Syntax and Semantics
6
Chapter 3: Syntax and Semantics
Issues (continued) Default exception handlers: Should they be provided? Specification of user-defined exceptions: Form, location, and scope Built-in exceptions: Can the user raise them explicitly? Disabling of exceptions: Should it be allowed? Chapter 3: Syntax and Semantics
7
Chapter 3: Syntax and Semantics
Exceptions in PL/I Conditions = exceptions Built-in and user-defined Default handlers for built-in conditions, but can be overridden Dynamic binding of handlers to exceptions Handlers are code segments, no parameters After handling exception, can send control anywhere. Default handlers go to raise of (cause) Chapter 3: Syntax and Semantics
8
Chapter 3: Syntax and Semantics
PL/I Example declare condition bad_input; . . . on condition bad_input begin; end; read(x); if (x < 0) or (x > 10) then signal condition bad_input; Chapter 3: Syntax and Semantics
9
Chapter 3: Syntax and Semantics
Exceptions in CLU More restrictive than PL/I Static binding of handlers to exceptions Handlers are attached to statements Exceptions must be handled by calling routine Unit raising exception is terminated; control transfers to statement following that with handler No disabling of exceptions Handlers can have parameters Exception failure raised if an exception has no handler Chapter 3: Syntax and Semantics
10
Chapter 3: Syntax and Semantics
CLU Example begin x := f(y); z := g(h); end except when bad_input(c): . . . f = proc (<formals>) signals(bad_input(char)) signal(bad_input(. . .)) Chapter 3: Syntax and Semantics
11
Chapter 3: Syntax and Semantics
Exceptions in Ada Less restrictive than CLU, more controlled than PL/I Static binding of handlers to exceptions If there is no local handler, exception is propagated up the call chain Handlers have no parameters Block that raises exception terminates, but enclosing block may continue execution. Disabling of exceptions possible Chapter 3: Syntax and Semantics
12
Chapter 3: Syntax and Semantics
Ada—Error Recovery procedure Sort ( x : in out Elem_Array ) is copy : Elem_Array := x; Make a copy of the array to be sorted begin -- Code here to sort the array X in ascending order -- Now test that the array is actually sorted for i in Elem_Array’First .. Elem_Array’Last-1 loop if x(i) > x(i + 1) then raise Sort_Error; -- a problem was detected; raise exception end if; end loop; exception when Sort_Error => x := copy; -- restore state and indicate a problem has arisen raise; when others => -- unexpected exception: Restore state and fail x := copy; raise Sort_Error; end Sort; Chapter 3: Syntax and Semantics
13
Chapter 3: Syntax and Semantics
Exceptions in C++ Based on earlier models, including Ada and CLU Uses “try blocks” Static binding of handlers to exceptions If there is no local handler, exception is propagated up the call chain Exceptions are objects (of any class, including user-defined) Handlers can have one parameter (the exception object) Try block that raises exception terminates, but block(s) enclosing activated handler continue execution Exceptions cannot be disabled Chapter 3: Syntax and Semantics
14
Chapter 3: Syntax and Semantics
C++ Try Block void Sort ( Elem_Array& x ) { Elem_Array copy( x ); // Make a copy of the array to be sorted try { // Code here to sort the array X in ascending order // Now test that the array is actually sorted for (int i := 0; i < x.length(); i++) if ( x(i) > x(i + 1) ) throw Sort_Error(); // a problem was detected; raise exc. } catch (Sort_Error& e) { x := copy; // restore state and indicate a problem has arisen throw; catch (...) { // unexpected exception: Restore state and fail x := copy; throw Sort_Error(); Chapter 3: Syntax and Semantics
15
Chapter 3: Syntax and Semantics
Summary Trade-offs between power, flexibility (PL/I) and safety (CLU). Ada provides a compromise But is exception handling really necessary? Arguments both ways (see Black, “Exception Handling: The Case Against”) Chapter 3: Syntax and Semantics
16
Working without Exception Handling
Two approaches: Pass a “status variable” or return an error code Pass a subroutine to be called under certain conditions In both cases, the exception handling is provided by the caller Tedious and error-prone; easy to omit checks To handle an exception locally, simply insert appropriate code Chapter 3: Syntax and Semantics
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.