Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Yoshi
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
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.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
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.
Introduction to Exceptions in Java. 2 Runtime Errors What are syntax errors? What are runtime errors? Java differentiates between runtime errors and exceptions.
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.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
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.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
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.
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.
06 - Exceptions. 2 ©S. Uchitel, 2004 A familiar sight? Bluescreen.scr.
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,
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
1 Exceptions and Concurrency Exceptions and Concurrency.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
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.
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.
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.
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 –
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.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
ICS 313: Programming Language Theory Chapter 14: Exceptions.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
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:
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….
Exception Handling and Event Handling
Introduction to Exceptions in Java
Exception Handling and Event Handling
Exception Handling and Event Handling
CSC 143 Java Errors and Exceptions.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors, other illegalities User-defined exceptions for robust abstractions Predefined exception raised by the runtime system. User-defined exception can be raised (thrown) by user code. Exception handlers specify remedial actions or proper shutdown Exceptions can be stored and re-raised later

concurrency - 2 Predefined exceptions in Ada Defined in Standard: Constraint_Error : value out of range Program_Error : illegality not detectable at compile-time: unelaborated package, exception during finalization... Storage_Error : allocation cannot be satisfied (heap or stack) Tasking _Error : communication failure Defined in Ada.IO_Exceptions: Data_Error, End_Error, Name_Error, Use_Error, Mode_Error, Status_Error, Device_Error

concurrency - 3 Handling exceptions 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 (“as expected”); when others => Put_Line (“out of the blue!”); end;

concurrency - 4 A common idiom with Integer_Text_Io; use Integer_Text_Io; function Get_Data return Integer is X : Integer; begin loop -- as long as input is not legal begin Get (X); return X; -- if got here, input is valid exception when others => Put_Line (“input must be integer, try again”); end; end loop; end;

concurrency - 5 Exception propagation When an exception is raised, the current sequence of statements is abandoned If an exception handler for the current exception is present, it is executed, and the current frame is completed Otherwise, the frame is discarded, and the enclosing dynamic scopes are examined to find a frame that contains a handler for the current exception If none is found, the program terminates The current frame is never resumed

concurrency - 6 User-defined Exceptions Client-server contract: if inputs are proper, either the output is correct or else client is notified of failure. The inputs are the responsibility of the client (the caller). package Stacks is Stack_Empty : exception; … package body Stacks is procedure Pop (X : out Integer; From : in out Stack) is begin if Empty (From) then raise Stack_Empty; else...

concurrency - 7 The scope of exceptions Exception has the same visibility as other declared entities: to handle an exception it must be visible in the handler An others clause can handle unamable exceptions partially when others => Put_Line (“disaster somewhere”); raise; -- propagate exception, program will terminate

concurrency - 8 Exception information An exception is not a type: we cannot declare exception variables and assign to them An exception occurrence is a value that can be stored and examined an exception occurrence may include additional information: source location of occurrence, contents of stack, etc. Predefined package Ada.Exceptions contains needed machinery.

concurrency - 9 Ada.Exceptions package Ada.Exceptions is type Exception_Id is private; type Exception_Occurrence is limited private; function Exception_Identity (X : Exception_Occurrence) return Exception_Id; function Exception_Name (X : Exception_Occurrence) return String; procedure Save_Occurrence (Target : out Exception_Occurrence; Source : Exception_Occurrence); procedure Raise_Exception (E : Exception_Id; Message : in String := “”)...

concurrency - 10 Using exception information exception when Expected : Constraint_Error => Save_Occurrence (Event_Log, Expected); when Trouble : others => Put_Line (“unexpected “ & Exception_Name (Trouble) & “ raised”); Put_Line (“shutting down”); raise;...

concurrency - 11 Exceptions in C++ Same runtime model Exceptions are classes Handlers appear in try blocks try { some_complex_calculation (); } catch (range_error) { // range error might be raised // in some_complex_calculation cerr << “oops\n”; catch (zero_divide) { // ditto for zero_divide cerr << “why is x zero?\n”; }

concurrency - 12 Defining and throwing exceptions The program throws an object. There is nothing in the declaration to indicate it will be used as an exception. struct Zero_Divide { public: int lineno; // useful information Zero_Divide () {…} // constructor }; … try { … if (x == 0) throw Zero_Divide (..); // call constructor and go

concurrency - 13 Exceptions and inheritance A handler names a class, and can handle an object of a derived class as well: class Matherr {}; // a bare object, no info class Overflow: public Matherr {…}; class Underflow: public Matherr {…}; class Zero_Divide: public Matherr {…}; try { weather_prediction_model (…); // who knows what will happen } catch (Overflow) {… // e.g. change parameters in caller catch (Matherr) { … // Underflow, Zero_Divide handled here catch (Disk_Error D) {… // Local name to capture exception info catch (…); // handle anything else (ellipsis)

concurrency - 14 Exceptions in Java Model and terminology similar to C++: exception are objects that are thrown and caught try blocks have handlers, which are examined in succession a handler for an exception can handle any object of a derived class Differences: all exceptions are extension of predefined class Throwable checked exceptions are part of method declaration the finally clause specifies clean-up actions

concurrency - 15 If a method might throw an exception, callers should know about it public void replace (String name, Object newvalue) throws NoSuch { Attribute attr := find (name); if (attr == null) throw new NoSuch (name); // build before throw newvalue.update (attr); } Caller must have a handler for NoSuch, or else must be declared as throwing NoSuch itself. Compiler checks. Only required for checked exceptions (not predefined ones, which are extensions of RuntimeException and Error).

concurrency - 16 Mandatory cleanup actions Some cleanups must be performed whether the method terminates normally or throws an exception. public void encrypt (String file) throws StreamException { Stream input; try { input = new Stream (file); iterator Words = new iterator (input); for (word w = Words.init (); Words.more(); w = Words.next()) { RSAencode(w); // may fail somewhere } finally { if (input != null) input.close(); }; // regardless of how we exit