Exception and Event Handling

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.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
Chapter 11 Exception Handling and Event Handling.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Preventing and Correcting Errors
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."
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.
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.
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.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
ISBN Chapter 14 Exception Handling and Event Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
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.
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
Exceptions In this lecture:
Why exception handling in C++?
Exception Handling and Event Handling
Part IX Fundamentals of C and C++ Programming Exception Handling
Chap 7. Building Java Graphical User Interfaces
Chapter 14: Exception Handling
Graphical User Interfaces -- Introduction
Exception Handling Chapter 9.
Java Programming Language
CIS16 Application Development Programming with Visual Basic
Exception Handling and Event Handling
Abdulmotaleb El Saddik University of Ottawa
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Introduction CSC 111.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exception Handling and Event Handling
Lecture 11 Objectives Learn what an exception is.
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
CMSC 202 Exceptions.
Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
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 and Event Handling (Based on:Concepts of Programming Languages, 8th edition, by Robert W. Sebesta, 2007)

Basic Concepts in Exception Handling I In the course of a program’s execution, many events may occur that were not expected by the programmer. We distinguish between two such classes of events: Those that are detected by hardware: e.g., disk read errors, end-of-file Those that are software-detectable: e.g., subscript range errors

Basic Concepts in Exception Handling II Definition: An exception is an unusual event that is detectable by either hardware or software and that may require special processing. Terminology: The special processing that may be required when an exception is detected is called exception handling. The processing is done by a code unit or segment called an exception handler. An exception is raised when its associated event occurs.

User-Defined Exception Handling When a language does not include specific exception handling facilities, the user often handles software detections by him/herself. This is typically done in one of three ways: Use of a status variable (or flag) which is assigned a value in a subprogram according to the correctness of its computation. [Used in standard C library functions] Use of a label parameter in the subprogram to make it return to different locations in the caller according to the value of the label. [Used in Fortran]. Define the handler as a separate subprogram and pass its name as a parameter to the called unit. But this means that a handler subprogram must be sent with every call to every subprogram.

Advantages to Built-in Exception Handling Without built-in Exception Handling, the code required to detect error conditions can considerably clutter a program. Built-in Exception Handling often allows exception propagation. i.e., an exception raised in one program unit can be handled in some other unit in its dynamic or static ancestry. A single handler can thus be used in different locations.

Advantages to Built-in Exception Handling Built-in Exception Handling forces the programmer to consider all the events that could occur and their handling. This is better than not thinking about them. Built-in Exception Handling can simplify the code of programs that deal with unusual situations. (Such code would normally be very convoluted without it).

Illustration of an Exception Handling Mechanism void example ( ) { … average = sum / total; return; /* Exception handlers */ When zero_divide average = 0; printf(“Error-divisor (total) is zero\n”); } The exception of division by zero, which is implicitly raised causes control to transfer to the appropriate handler, which is then executed

Design Issues for Exception Handling I: Exception Binding Binding an exception occurrence to an exception handler: At the unit level: how can the same exception raised at different points in the unit be bound to different handlers within the unit? At a higher level: if there is no exception handler local to the unit, should the exception be propagated to other units? If so, how far? [Note: if handlers must be local, then many need to be written. If propagation is permitted, then the handler may need to be too general to really be useful.]

Design Issues for Exception Handling II:Continuation After an exception handler executes, either control can transfer to somewhere in the program outside of the handler code, or program execution can terminate. Termination is the simplest solution and is often appropriate. Resumption is useful when the condition encountered is unusual, but not erroneous. In this case, some convention should be chosen as to where to return: At the statement that raised the exception? At the statement following the statement that raised the exception? At some other unit?

Design Issues for Exception Handling III: Others Is finalization—the ability to complete some computations at the end of execution regardless of whether the program terminated normally or because of an exception—supported? How are user-defined exceptions specified? Are there pre-defined exceptions?

Design Issues for Exception Handling III: Others Should it be possible to disable predefined exceptions? If there are pre-defined exceptions, should there be default exception handlers for programs that do not provide their own? Can pre-defined exceptions be explicitly raised? Are hardware-detectable errors treated as exceptions that may be handled?

Exception Handling in Java: Class Hierarchy for Exceptions Usually thrown by JVM when a user program causes an error Errors thrown by the JVM Errors never thrown by user programs and should never be handled there

Introduction to Event Handling I Event handling is similar to exception handling. The difference is that while exceptions can be created wither explicitly by user code or implicitly by hardware or a software interpreter, events are created by external actions, such as user interactions though a graphical user interface (GUI) In event-driven programming, parts of the program are executed at completely impredictable times, often triggered by user interactions with the executing program.

Introduction to Event Handling II An event is a notification that something specific has occurred, such as a mouse click on a graphical button. An event handler is a segment of code that is executed in response to the appearance of an event.

Introduction to Event Handling II Event handling is useful in Web applications such as: commercial applications where a user clicks on buttons to select merchandise or Web form completion, where event handling is used to verify that no error or omission has occurred in the completion of a form. Java supports two different approaches to presenting interactive displays to users: either from application programs or from applets.

Thanks