Exceptions (Large parts of these copied from Ed Schonberg’s slides)

Slides:



Advertisements
Similar presentations
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
Advertisements

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.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
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.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Chapter 8 Exceptions. Topics Errors and Exceptions try-catch throwing Exceptions Exception propagation Assertions.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
1 Exceptions and Concurrency Exceptions and Concurrency.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Java Software Solutions Foundations of Program Design Sixth Edition
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."
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
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.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
CIS 270—Application Development II Chapter 13—Exception 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 CSC241: Object Oriented Programming Lecture No 27.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
1 Exception handling Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 11.1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Java Exceptions a quick review….
PZ11A Programming Language design and Implementation -4th Edition
Chapter 9 :: Subroutines and Control Abstraction
Exceptions with Functions
Chapter 12 Exception Handling and Text IO
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
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.
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:

Exceptions (Large parts of these copied from Ed Schonberg’s slides)

Exceptions General mechanism for handling abnormal conditions Predefined exceptions: Constraint violations, I/O errors, other illegalities User-defined exceptions Exception handlers specify remedial actions or proper shutdown.

From the programmer’s point of view this is a hard construct to use. –Where should an exception be handled –What information is needed to handle it? Often necessary to combine local information (what error was raised?) with global context (what should be done?). Difficult for formal analysis. However, for our purposes (PL), not so bad.

Exception propagation In Ada, C++, Java, handlers are attached to statements. If the current statement has a handler for this exception, execute it. Else move lexically outward, until finding a handler. If the routine has no suitable handler, go to calling statement. Iterate. If you reach the top level, abort the program.

Exceptions in Ada Any begin-end block can have an exception handler: procedure test is x : integer = 25; y : integer = 0; begin x := x/y; exception when Constraint_Error => Put_Line(“Divided by Zero”) when others => Put_Line(“Other bug”) end

A Common Idiom function Get_Data return integer is X: Integer begin loop begin Get(X); return X; exception when others => Put_Line(“Input must be integer”); end; end loop; end;

User-defined exception An exception is a declared identifier, with usual scoping rules. package Stacks is Stack_Empty: exception; package body Stacks is procedure Pop(X : out integer; From: in out Stack) begin if Empty(From) then raise Stack_Empty; …

Exception occurrence Built in package “Ada.occurence” defines an “exception occurrence” as a type that can hold information like location of occurrence, stack contents, etc. Built in function “Save_Occurrence” saves this information. Executed as part of handler.

Exceptions in C++ Exceptions are classes. Handlers appear in try blocks try { ComplexCalculation(); } catch (range_error) { cerr << “range error\n”; } catch (zero_divide) { cerr << “why is x zero?\n”; } catch (…) { cerr << “What’s going on?”; } “…” is the actual C++ code here.

Defining and throwing exceptions A program throws an object. The declaration does not indicate it will be used as an exception. class selfIntersectErr { Polygon P; // useful information selfIntersectErr () { …} // constructor { … if (P.SelfIntersects()) throw selfIntersectErr(); … }

Exceptions and inheritance A handler names a class and can handle an object of a derived class.

Exceptions in Java Similar to C++ –Exceptions are objects that are thrown and caught. –Try blocks have handlers, which are examined in succession. –A handler can handle an object of a derived class. Differences –All exceptions are part of predefined class Throwable. –“finally” clause is executed at the end of a “try” block, whether normally executed or handled by a handler. –Method must declare all exceptions that it may pass onto a caller.

public void replace (String name, Object newvalue) throws NoSuch { Attribute attr = find(name); if (attr==null) throw new NoSuch(name); … } Caller must either have a handler for NoSuch or be declared as throwing it. Compiler checks. (Hence awkwardness of reading in Java.)

Implementation Simple implementation: Stack of handlers and subroutine calls. Each handler indicates exceptions it handles and address of statement after try block. When enter “try” block add handlers to stack in backward order. When call routine, push call (part of calling sequence). When exit, pop them.

When an exception is raised global variable E; // raised exception while (HandlerStack is not empty) { H = pop(HandlerStack); if (H handles E) { execute body of H; E = null; jump to return address of H; } if (H is a subroutine) execute epilogue of H; } abort program;

In epilogue of each subroutine Just before jump to return address of caller add: if (E != null) jump to exception handler;

Inefficiency The above procedure requires handlers to be enqueued and dequeued each time a try block is entered. Large runtime cost for rarely used functionality. Scott describes a cleverer implementation with no runtime cost until exception is actually raised.

Implementation of interrupt- based exceptions? For exceptions based on hardware interrupts (signal from I/O device, divide by zero, overflow, underflow, etc.), the interrupt handler in the OS has to transfer control and information to the exception handler in the runtime system. I don’t know how this works.