Exception Handling In Text: Chapter 14.

Slides:



Advertisements
Similar presentations
Exception Handling and Event Handling
Advertisements

Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations – this.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
Chapter 14 Exception Handling and Event Handling.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 14 Exception Handling and Event Handling.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
Chapter 11 Exception Handling and Event Handling.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
PZ11A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ11A - Exception handling Programming Language Design.
Chapter 13, Slide 1 Exception Handling Exception handling is a language feature that allows the programmer to handle runtime "exceptional conditions."
Chapter 14 Exception Handling and Event Handling.
1 Exceptions (Section 8.5) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
ISBN Chapter 14 Exception Handling and Event Handling.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
ISBN Chapter 14 Exception Handling and Event Handling.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations this.
Exception Handling C++.
Exceptions Error Handling and Recovery
Exception Handling and Event Handling
Exception Handling and Event Handling
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
PZ11A Programming Language design and Implementation -4th Edition
Exception and Event Handling
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Java Programming Language
Exception Handling and Event Handling
Chapter 14: Exception Handling
Chapter 9 :: Subroutines and Control Abstraction
Designing with Java Exception Handling
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chapter 17 Templates and Exceptions Part 2
Exception Handling and Event Handling
Exception Handling Chapter 9 Edited by JJ.
COP4020 Programming Languages
Data Abstraction: The Walls
Part B – Structured Exception Handling
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exception Handling and Event Handling
Languages and Compilers (SProg og Oversættere)
Designing with Java Exception Handling
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Ninth step for Learning C++ Programming
Exception Handling and Event Handling
Tenth step for Learning C++ Programming
Exception and Event Handling
Exception Handling and Event Handling
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception Handling and Event Handling
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
Presentation transcript:

Exception Handling In Text: Chapter 14

 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 

 Chapter 3: Syntax and Semantics  Examples What are examples of exceptional conditions?  Chapter 3: Syntax and Semantics 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

 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 

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 