Exception Handling In Text Chapter 14 1 Exception
- Slides: 16
Exception Handling In Text: Chapter 14 1
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 2
Examples What are examples of exceptional conditions? Chapter 3: Syntax and Semantics 3
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 4
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 5
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 6
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 7
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 8
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 9
CLU Example begin x : = f(y); z : = g(h); end except when bad_input(c): . . . end f = proc (<formals>) signals(bad_input(char)) begin . . . signal(bad_input(. . . )) . . . Chapter 3: Syntax and Semantics 10
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 11
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 12
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 userdefined) 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 13
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 14
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 15
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 16
- Making connections images
- Contoh error handling
- Exception handling in vb
- Exception handling in java
- Apa itu exception
- Exception handling in php example
- Exception handling pada java
- Fiq irq
- Exception handling in ada
- Php throw exception
- Exception handling in vb.net
- Chapter 18 handling social pressures
- Chapter 20:1 operating the microscope
- Chapter 13 handling emergencies
- Chapter 2 camera handling care and support
- Chapter 10 handling stress
- Extends and implements difference