1 / 25

Mastering Exception and Event Handling in Programming

Explore the basics, models, and advantages of exception handling. Learn about design issues, control flow, and models in C++. Discover the similarities and differences with event handling. Dive into error detection, propagation, and handling alternatives.

ingramg
Download Presentation

Mastering Exception and Event Handling in Programming

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. Chapter 11 Exception Handling and Event Handling

  2. Content • 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

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

  4. 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. • exceptionhandler the complete system of software routines, which several all exception and pass control .

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

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

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

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

  9. Exception Handling Control Flow

  10. Exceptionhandling models1.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.)

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

  12. 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 }

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

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

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

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

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

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

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

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

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

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

  23. Event Classes • Semantic Event Classes • ActionEvent • ItemEvent • TextEvent • Lower-Level Event Classes • ComponentEvent • KeyEvent • MouseEvent • MouseMotionEvent • FocusEvent

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

  25. Summary • 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.

More Related