EXCEPTION HANDLING AND EVENT HANDLING
Exception handling is a special processing that may required after detection of an exception. Exception handling code unit is called an exception handler. There are many program that allow program to trap input/output errors(including EOF). An exception is any usual event, either erroneous or not, detectable by either hardware or software, that may require special processing.
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, program are allowed to trap some exceptions, thereby providing the possibility of fixing the problem and continuing.
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
C++ exception handlers,
- Exception Handlers Form:
try {
— code that is expected to raise an exception
}
catch (formal parameter) {
— handler code
}
…
catch (formal parameter) {
— handler code
}
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 exception,
- 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
Event handling, an event is a notification that something specific has occurred, such as a mouse click on a graphical button. Event handler is a segment of code that is executed in response to an event.