Session 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
so we can conclude that programmers need exception handling. Exception handling lets programs to trap some exceptions, thereby providing the possibility of fixing the problem and continuing.
Basic Concepts
* Many languages allow programs to trap input/output errors (including EOF)
* An exception is any unusual event, either erroneous or not, detectable by either hardware or software, that may require special processing
* The special processing that may be required after detection of an exception is called exception handling
* The exception handling code unit is called an exception handler
Exception Handling Alternatives
* An exception is raised when its associated event occurs
* A language that does not have exception handling capabilities can still define, detect, raise, and handle exceptions (user defined, software detected)
* 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 subprogram
There are some advantages of Built-in Exception Handling :
* Error detection code is tedious to write and it clutters the program
* Exception handling encourages programmers to consider many different possible errors
* Exception propagation allows a high level of reuse of exception handling code
Exception Handling Control Flow











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

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
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 default handler is called
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
* Other design choices
     - All exceptions are user-defined
     - Exceptions are neither specified nor declared
     - The default handler, unexpected, simply terminates the program; unexpected can be redefined by the user
     - Functions can list the exceptions they may raise
     - Without a specification, a function can raise any exception (the throw clause)

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

In Java, there are some GUI components such as text box, radio button, and layout manager objects which are placed in a frame. and event handling is also available at C#.


differences between exception handling in Java and in C++. For short, the differences are :
1. C++ and Java both have try and catch blocks, but Java have one new block finally which always executed after try and catch.
2. Java throw only objects but C++ throws data( primitive and pointer & objects ).
3. C++ have catch(...) {    } to catch all exception of all kinds and Java have catch(Exception e) {    } to catch all exception

Komentar