1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.

Slides:



Advertisements
Similar presentations
Exception Handling and Event Handling
Advertisements

Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
PZ09B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09B - Parameter transmission Programming Language Design.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Exception Handling Exception handling (EH) allows a programmer to provide code in the program to handle run-time errors or exceptional situations – this.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Chapter 14 Exception Handling and Event Handling.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Exception Handling. Background The fact that software modules should be robust enough to work under every situation. The exception handling mechanism.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
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)
Exceptions COMPSCI 105 S Principles of Computer Science.
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.
Object Oriented Programming
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
ISBN Chapter 14 Exception Handling and Event Handling.
PZ06C Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06C - Polymorphism Programming Language Design and.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
ICS 313: Programming Language Theory Chapter 14: Exceptions.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exceptions in OO Programming Introduction Errors Exceptions in Java Handling exceptions The Try-Catch-Finally mechanism Example code Exception propagation.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
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
Chapter 9 :: Subroutines and Control Abstraction
Exception Handling and Event Handling
Exception Handling In Text: Chapter 14.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
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 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
CMSC 202 Exceptions.
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:

1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1

2 Exception handling Events in a program sometimes occur at unpredictable times, I.e., apparently randomly. That is, the occurrence is an exception to the normal sequencing of events. Such events are called exceptions, and programming language constructed designed to handle these are called exception handlers. Exceptions can be created by the hardware or by software: Examples Power failureHardware generated Printer out of paper End of page Divide by 0 End of array reachedSoftware generated

3 Testing for exceptions Although software exceptions can be tested in the code, the existence of an exception handling mechanism relieves the programmer the burden of constantly checking for this condition. Causing an exception: to raise an exception or throw an exception. Example: End-of-page Without exceptions, need to test at each print statement: print data; if end-of-page then call newPage(); With automatic exceptions, setup mechanism: [PL/I example:] On endpage begin put page; end; /* Resume execution */ can write output without checking end-page condition each time

4 Implementation of exceptions Exceptions come from two sources: conditions detected by the virtual machine, and conditions generated by the semantics of the programming language. In the former case, operating system exceptions may be raised directly by hardware interrupts or traps, such as arithmetic overflow, or they may be raised in support software, such as end-of-file condition. In C, the programmer has direct access to these signals processed by the operating system. The programmer may enable an interrupt (e.g., the sigaction function in Unix, which specifies a procedure that gets invoked when the given signal is raised).

5 Exceptions in ML One can throw an exception and a handler will catch the exception. (More limited than PL/I example. Need to explicitly raise an exception.): fun doDivide(a,b) = if b=0 then raise badDenominator else a/b; fun divide(a,b) = doDivide(a,b) handle badDenominator => (print “Divide error”; 0.0); Note: doDivide is within dynamic scope of divide. Semantics if exception is raised: - If no handler, end function - Check dynamic scope of executing procedures to look for appropriate handler - If handler, execute handler, then return to exception point (e.g., print message and return 0.0 as default value in above example.)

6 Exceptions in C++ try clause implements exceptions. Similar to ML: try { statements... If b=0 throw badDenominator; } catch badDenominator {Do something;}; Propagating exceptions: If no local exception handler, who catches exception? Usually dynamic links checked for handler try statement terminates after handling exception.

7 Exceptions in Ada procedure Sub is Bad_Data_Value: exception; -other declarations for Sub begin -statements for normal processing in Sub raise Bad_Data_Value -- to raise this exception exception when Bad_Data_Value => -handler for bad data values when Constraint_Error => -handler for predefined exception Constraint_Error when others => -handler for all other exceptions end; [Constraint_Error (e.g., number too large) is a built-in exception in Ada.]

8 Propagating exceptions the place at which an exception occurs is not the best place to handle it a particular exception is usually defined in terms of the dynamic chain of subprogram activations each activation record needs to record those active exceptions being passed from calling procedure propagating exceptions allows a subprogram to remain as a programmer-defined abstract operation even in processing exceptions: a subprogram may interrupt its normal processing and raise an exception. to the caller, the effect of a subprogram's raising an exception is the same as a primitive operation's raising an exception if the subprogram does not handle the exception. if the exception is handled within the subprogram, then the subprogram returns in the normal way, and the caller is never aware that an exception has been raised.

9 More on implementation of exceptions the language translator may insert additional instructions. For example, to detect Index_Check caused by an invalid array subscript, the translator inserts instructions at each reference to an array, such as A[I,J], to determine whether the values of I and J are within the declared bounds. Unless the hardware or operating system provides the exception checking, checking for an exception requires software simulation. Often this cost is large. For example, it may take longer to perform the subscript bounds check on A[I,J] than it does to access the element of the array. Because of this extra cost, most languages provide a means to turn off checking for exceptions in parts of the program where the programmer determines it is safe to do so [e.g., pragma Supress(Index_Check) in Ada]. Note: programmers are notoriously bad in determining when it is safe to do so.

10 Assertions An assertion is a predicate that checks for an invalid value and then raises an exception: In C++: #include... assert(x,y); In C++ it is really a macro that generates: if (x>y) {/* Print error message */} (Compare assertions to pre- and postconditions of axiomatic verification in Section )