1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)

Slides:



Advertisements
Similar presentations
1 Exceptions: An OO Way for Handling Errors Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software.
Advertisements

Exception Handling and Event Handling
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
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.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
ISBN Chapter 14 Exception Handling and Event Handling.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 11 Exception Handling and Event Handling.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
© 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.
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
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.
Object Oriented Programming
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.
Slides Credit Umair Javed LUMS Web Application Development.
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.
COP4020 Programming Languages Exception Handling Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
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.
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 and Event Handling
Exception Handling and Event Handling
Exception and Event Handling
Java Programming Language
Exception Handling and Event Handling
Exception Handling and Event Handling
Exception Handling Chapter 9 Edited by JJ.
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 Handling and Event Handling
Exception and Event Handling
Exceptions 10-May-19.
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.
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 and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)

2 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

3 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.

4 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.

5 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. 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).

6 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

7 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.]

8 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?

9 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? 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?

10 Exception Handling in Java: Class Hierarchy for Exceptions Throwable Error Run Out of Heap Memory Exception Runtime Exception Out of Bound Exception Null Pointer Exception IO Exception Errors thrown by the JVM Errors never thrown by user programs and should never be handled there Usually thrown by JVM when a user program causes an error

11 Exception Handling in Java: Exception Handlers A try construct includes a compound statement called the try clause and a list of exception handlers: try { //** Code that is expected to raise an exception } catch (formal parameter) { //* A handler body } … catch (formal parameter) { //** A handler body }

12 Exception Handling in Java: Binding Exceptions to Handlers An exception is thrown using the throw statement. E.g.: throw new MyException (“a message to specify the location of the error”) Binding: If an exception is thrown in the compound statement of a try construct, it is bound to the first handler (catch function) immediately following the try clause whose parameter is the same class as the thrown object, or an ancestor of it. If a matching handler is found, the throw is bound to it and it is executed.

13 Exception Handling in Java: The finally clause Sometimes, a process must be executed regardless of whether an exception is raised or not and handled or not. This occurs, for example, in the case where a file must be closed or an external resource released, regardless of the outcome of the program. This is done by adding a finally clause at the end of the list of handlers, just after a complete try construct. The finally clause is executed in all cases whether or not try throws an exception, and whether or not it is caught by a catch clause.

14 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.

15 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. 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.