1 / 15

Exception and Event Handling

Exception and Event Handling. Credits Robert W. Sebesta, Concepts of Programming Languages , 8 th ed., 2007 Dr. Nathalie Japkowicz. Basic Concepts in Exception Handling (1). In the course of a program’s execution, events may occur that are not normally expected in a typical run.

corrineg
Download Presentation

Exception and Event Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8th ed., 2007 Dr. Nathalie Japkowicz

  2. Basic Concepts in Exception Handling (1) • In the course of a program’s execution, events may occur that are not normally expected in a typical run. • We distinguish two classes: • events that are detected by hardware, e.g., a disk read error or a premature end-of-file; • events that can be detected by software, e.g., some type of arithmetic errors or subscript range errors.

  3. Basic Concepts in Exception Handling (2) • Definition An exception is an unusual (but generally expected!) event that can be detected either by hardware or by software and that may require special processing. • Terminology The special processing that may be required when an exception is detected is called exception handling. It 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 users often handle software detection (messily) on their own: • a status variable (or flag) is assigned a value in a subprogram according to the correctness of its computation (standard C library functions) • a label parameter in the subprogram makes it return to different locations in the caller according to the value of the label (Fortran) • the handler is a separate subroutine and its name must be passed as a parameter to the called unit (so, the handler must be made known to every subprogram called)

  5. Advantages of Built-in Exception Handling • Without it, the code that detect error conditions or unusual situations can considerably clutter a program. • It often allows exception propagation: an exception raised in one program unit can be handled in some other unit in its dynamic or static ancestry. A single handler can work in different locations. • It forces the programmer to consider all events that could occur and how to handle them. This sure beats not thinking about them.

  6. Example of an Exception Handling Mechanism void p( ){ -- float average, sum, total; ... average = sum / total; ... -- exception handlers when zero_divide { average = signum(sum) * 3.40282347e+38f; -- and print an error message? } ... } When the exception of division by zero is implicitly raised, the appropriate handler takes over.

  7. Design Issues for Exception Handling: Exception Binding Binding an exception occurrence to an exception handler: • At the unit level: how can an 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. On the other hand, if propagation is permitted, then the handler may need to be too general to really be useful.]

  8. Design Issues for Exception Handling: Continuation After an exception handler has been done: • control transfers to somewhere in the program, outside the handler code, • or the program stops. Termination, the simplest solution, is often appropriate. Resumption is useful when the condition encountered is unusual, but not erroneous. In such a case, a convention should be chosen as to where to return: • to the statement that raised the exception • to the statement following the exception raiser • to another unit

  9. Design Issues for Exception Handling: Others • Is finalization supported? That’s the ability to complete some computations at the end of execution regardless of whether the program terminated normally or because of an exception. • 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 handlers? • Can pre-defined exceptions be explicitly raised? • Are hardware-detectable errors treated as exceptions that may be handled?

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

  11. Exception Handling in Java: Exception Handlers The 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, for example: 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 finallyclause • Sometimes, a process must be executed regardless of whether an exception is raised or not and handled or not. • This occurs, for example, when a file must be closed or an external resource released, regardless of the outcome of the program. • This is done by adding a finallyclause 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 (1) • Event handling is similar to exception handling. • Exceptions can be created either explicitly by user code or implicitly by hardware or a software interpreter.Events are created by external actions, such as user interactions through a Graphical User Interface. • In event-driven programming, parts of the program are executed at completely unpredictable times, often triggered by user interactions with the executing program.

  15. Introduction to Event Handling (2) • 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 occurrence of an event. • Event handling is useful in Web applications such as: • commercial applications where a user clicks on buttons to select merchandise, • Web form completion, where event handling is used to verify that no error or omission has been made in the completion of a form. • Java supports two different approaches to presenting interactive displays to users: either from application programs or from applets.

More Related