Chapter 13, Slide 1 Exception Handling Exception handling is a language feature that allows the programmer to handle runtime "exceptional conditions."

Slides:



Advertisements
Similar presentations
Exception Handling and Event Handling
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
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 Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
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.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
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.
VBA Modules, Functions, Variables, and Constants
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
ISBN Chapter 14 Exception Handling and Event Handling.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11 Exception Handling and Event Handling.
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Chapter 11: Distributed Processing Variations of subprogram control
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.
May 21, ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 Subroutines and Control Abstraction Aaron Bloomfield CS 415 Fall 2005.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
(12.1) COEN Concurrency and Exceptions  Coroutines  Physical and logical concurrency  Synchronization – semaphores – monitors – message passing.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
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 and Event Handling
Exception Handling and Event Handling
PZ11A Programming Language design and Implementation -4th Edition
Exception and Event Handling
Exception Handling and Event Handling
PL/SQL Scripting in Oracle:
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Exception Handling and Event Handling
Exception Handling In Text: Chapter 14.
Architectural Support for OS
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exception Handling and Event Handling
Languages and Compilers (SProg og Oversættere)
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
Architectural Support for OS
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
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:

Chapter 13, Slide 1 Exception Handling Exception handling is 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 13, Slide 2 Terminology An exception is raised, or signalled, 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 13, Slide 3 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 13, Slide 4 Issues (continued) Default exception handlers.  Should they be provided? Specification of user-defined exceptions.  Form, location, scope. Built-in exceptions.  Can the user raise them explicitly? Disabling of exceptions.  Should it be allowed?

Chapter 13, Slide 5 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 or cause.

Chapter 13, Slide 6 PL/I Example declare condition bad_input;... on condition bad_input begin;... end;... read(x); if (x 10) then signal condition bad_input;

Chapter 13, Slide 7 Exceptions in CLU More restricted 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 13, Slide 8 CLU Example begin x := f(y); z := g(h); end except when bad_input(c):... end f = proc ( ) signals(bad_input(char)) begin... signal(bad_input(...))...

Chapter 13, Slide 9 Exceptions in Ada Less restrictive than CLU, more controlled than PL/I Static binding of handlers to exceptions, but if no local handler, exception is propagated back call chain Handlers have no parameters Block that raises exception terminates, but enclosing block may continue execution. Disabling of exceptions possible

Chapter 13, Slide 10 Ada — Error Recovery procedure Sort (X: in out ELEM_ARRAY ) is Copy: ELEM_ARRAY ; begin -- Take a copy of the array to be sorted. for i in ELEM_ARRAY’RANGE loop Copy (i) := X (i) ; end loop ; -- 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 -- a problem has been detected - raise exception raise Sort_error ; end if ; end loop ; exception -- restore state and indicate to calling procedure -- that a problem has arisen when Sort_error => for i in ELEM_ARRAY’RANGE loop X (i) := Copy (i) ; end loop ; raise ; -- unexpected exception. Restore state and indicate -- that the sort has failed when Others => for i in ELEM_ARRAY’RANGE loop X (i) := Copy (i) ; end loop ; raise Sort_error; end Sort ;

Chapter 13, Slide 11 Summary Trade-offs between power, flexibility (PL/I) and safety (CLU).  Ada provides a compromise. But is exception handling really necessary?  Arguments both ways (Black, "Exception Handling: The Case Against")

Chapter 13, Slide 12 Handling Exceptions without Exception Handling Two approaches:  Pass a "status variable."  Pass a subroutine to be called under certain conditions. In both cases, the exception handling is provided by the caller. To handle an exception locally, simply insert appropriate code.