Chapter 11 Exception Handling and Event Handling.

25
Chapter 11 Chapter 11 Exception Handling and Event Handling
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    265
  • download

    6

Transcript of Chapter 11 Exception Handling and Event Handling.

Page 1: Chapter 11 Exception Handling and Event Handling.

Chapter 11Chapter 11

Exception Handling and Event

Handling

Page 2: Chapter 11 Exception Handling and Event Handling.

ContentContent •Introduction to Exception Handling•Basic Concepts•Exception Handling Alternatives•Advantages Exception Handling•Design Issues•Exception handling models•Exception Handling in C++•Introduction to Event Handling•Basic concepts•Event Handling With Java•Summary

Page 3: Chapter 11 Exception Handling and Event Handling.

Introduction to Exception Handling

• In a language without exception handling– When an exception occurs, control goes to the

operating system, where a message is displayed and the program is terminated

• In a language with exception handling– Programs are allowed to trap some exceptions,

thereby providing the possibility of fixing the problem and continuing

Page 4: Chapter 11 Exception Handling and Event Handling.

Basic Concepts• An exception is any unusual event, either erroneous

or not, detectable by either hardware or software, that may require special processing.

• exception handling is programming language construct or computer hardware mechanism designed to handle the occurrence of some condition that changes the normal flow of exception.

• exception handler the complete system of software routines, which several all exception and pass control .

Page 5: Chapter 11 Exception Handling and Event Handling.

Exception Handling Alternatives

• Alternatives:– Send an auxiliary parameter or use the return

value to indicate the return status of a subprogram

– Pass a label parameter to all subprograms (error return is to the passed label)

– Pass an exception handling subprogram to all subprograms

Page 6: Chapter 11 Exception Handling and Event Handling.

Advantages To Having Exception Handling Built

• Error detection code is tedious to write and it clutters the program.

• Exception propagation allows a high level of reuse of exception handling code.

• Dealing with no erroneous but unusual situation can be simplified.

Page 7: Chapter 11 Exception Handling and Event Handling.

Design Issues

• How and where are exception handlers specified and what is their scope?

• How is an exception occurrence bound to an exception handler?

• Where does execution continue, if at all, after an exception handler completes its execution?

• How are user-defined exceptions specified?

Page 8: Chapter 11 Exception Handling and Event Handling.

Design Issues

• Should there be default exception handlers for programs that do not provide their own?

• Can built-in exceptions be explicitly raised?• Are hardware-detectable errors treated as

exceptions that can be handled?• Are there any built-in exceptions?• How can exceptions be disabled, if at all?

Page 9: Chapter 11 Exception Handling and Event Handling.

Exception Handling Control Flow

Page 10: Chapter 11 Exception Handling and Event Handling.

Exception handling models

1. Termination model: a signaler is terminated after raising an exception.2. Strict-resumption model: a signaler must be resumed after the raised exception is handled.3. Resumption model: a signaler may be resumed after the raised exception is serviced. (depends on defined action for each possible exception.)4. Combination model: a signaler can decide which of the other signaling conventions it wants to follow when it raises an exception. (Up to the programmer what to do.) 

Page 11: Chapter 11 Exception Handling and Event Handling.

Exception Handling in C++

• Added to C++ in 1990.• c++ uses a special construct that is introduced

with the reserved word try for this purpose .a try construct includes compound statement called the try clause and a list of exception handlers.

Page 12: Chapter 11 Exception Handling and Event Handling.

C++ Exception Handlers

• Exception Handlers Form:try {-- code that is expected to raise an exception}catch (formal parameter) {-- handler body}...catch (formal parameter) {-- handler body}

Page 13: Chapter 11 Exception Handling and Event Handling.

The catch Function

• catch is the name of all handlers--it is an overloaded name, so the formal parameter of each must be unique.

• The formal parameter need not have a variable– It can be simply a type name to distinguish the

handler it is in from others.• The formal parameter can be used to transfer

information to the handler.• The formal parameter can be an ellipsis, in which

case it handles all exceptions not yet handled.

Page 14: Chapter 11 Exception Handling and Event Handling.

Throwing Exceptions

• Exceptions are all raised explicitly by the statement:throw [expression];

• The brackets are metasymbols.• A throw without an operand can only appear in

a handler; when it appears, it simply re-raises the exception, which is then handled elsewhere.

• The type of the expression disambiguates the intended handler.

Page 15: Chapter 11 Exception Handling and Event Handling.

Unhandled Exceptions

• An unhandled exception is propagated to the caller of the function in which it is raised.

• This propagation continues to the main function.• If no handler is found, the program is terminated.

Page 16: Chapter 11 Exception Handling and Event Handling.

Continuation

• After a handler completes its execution, control flows to the first statement after the last handler in the sequence of handlers of which it is an element.

Page 17: Chapter 11 Exception Handling and Event Handling.

Evaluation

• It is odd that exceptions are not named and that hardware- and system software-detectable exceptions cannot be handled.

• Binding exceptions to handlers through the type of the parameter certainly does not promote readability.

example

Page 18: Chapter 11 Exception Handling and Event Handling.

Introduction to Event Handling

• Event handling is similar to exception handling

• An event is created by an external action such as a user interaction through a GUI.

Page 19: Chapter 11 Exception Handling and Event Handling.

Basic concepts

•An event is a notification that something specific has occurred , such as a mouse click on a button . Strictly speaking , an event is an object that is implicitly created by the run –time system in response to a user action , at least in the context in which event handling is being discussed here .

•An event handler is a segment of code that Is executed in response to the appearance of an event . Event handlers enable a program to be responsive to user action .

Page 20: Chapter 11 Exception Handling and Event Handling.

Event Handling With Java

Java supports two different approaches to presenting interactive displays to users:-

1.  from applications programs.

2. from applets .

Both use the same classes to define the GUI components and the event handlers that provide interactivity .

Page 21: Chapter 11 Exception Handling and Event Handling.

Java Swing GUI Components

• Text box is an object of class JTextField• Radio button is an object of class JRadioButton

• Applet’s display is a frame, a multilayered structure

• Content pane is one layer, where applets put output• GUI components can be placed in a frame• Layout manager objects are used to control the

placement of components

Page 22: Chapter 11 Exception Handling and Event Handling.

The Java Event Model• User interactions with GUI components create

events that can be caught by event handlers, called event listeners

• An event generator tells a listener of an event by sending a message

• An interface is used to make event-handling methods conform to a standard protocol

• A class that implements a listener must implement an interface for the listener

Page 23: Chapter 11 Exception Handling and Event Handling.

Event Classes

• Semantic Event Classes– ActionEvent– ItemEvent– TextEvent

• Lower-Level Event Classes– ComponentEvent– KeyEvent– MouseEvent– MouseMotionEvent– FocusEvent

Page 24: Chapter 11 Exception Handling and Event Handling.

Summary• c++ includes predefined exceptions that can

be implicitly corporate to other program entities. The throw statement handlers have the same name catch.

• the c++ throw clause of a method lists all types of exception that the method could throw but not handle.

Page 25: Chapter 11 Exception Handling and Event Handling.

•In case something has occurred by an event that requires handling, which is created by user interactions with a program,usually through a graphic user interface.

•Java event handlers are call event listeners the who must be registered for an event especially when the event occurs .two of the most commonly event listener are action performed and itemstatechang whose protocols are provided by associated interfaces.

Summary