Chapter 13 Exception Handling 1. Some common examples of exceptions are an out-of-bounds array subscript, arithmetic overflow, division by zero, invalid function parameters and determining that there is insufficient memory to satisfy an allocation request by new. 2. The spirit behind exception handling is to enable programs to catch and handle errors rather than letting them occur and simply suffering the consequences. With exception handling, if the programmer does not provide a means of handling a fatal error, the program will terminate; nonfatal errors normally allows a program to continue executing, but produce incorrect results. 3. Exception handling is designed for dealing with synchronous errors, ie., errors that occur as the result of a program's execution. 4. Exception handling is not designed to deal with asynchronous situations such as network message arrivals, disk I/O completions, mouse clicks, and the like; these are best handled through other means, such as interrupt handling. 5. Exception handling is typically used in situations in which the error will be dealt with by a different part of the program (ie a different scope) from that which detected the error. 6. Exceptions should not be used as an alternate mechanism for specifying flow of control. Flow of control with conventional control structures is generally clearer and more efficient than with exception. 7. Exception handling should be used to process exceptions for program components that are not geared to handling those exceptions directly. 8. Exception handling should be used to process exceptions from software components such as functions, libraries, and classes that are likely to be widely used, and where it does not make sense for those components to handle their own exceptions. 9. Exception handling should be used on large projects to handle error processing in a uniform manner for the entire project. 10. C++ Exception handling is geared to situations in which the function that detects an error is unable to deal with it. Such a function will throw an exception. If the exception matches the type of the parameter in one of the catch blocks, the code for that catch block is executed. Otherwise, function terminate is called, which by default calls function abort. 11. The programmer enclosed in a try block the code that may generate an error that will produce an exception. The try block is immediately followed by one or more catch blocks. Each catch block specifies the type of exception it can catch and handle. Each catch block contains an Exception handler. 12. Program control on a thrown exception leaves the try block and search the catch blocks in order for an appropriate handler. If no exceptions are thrown in the try block, the exception handlers for that block are skipped and the program resumes exception after the last catch block. 13. Exceptions are thrown in a try block in a function or from a function called directly or indirectly from the try block. 14. Once an exception is thrown, control cannot return directly to the throw point. 15. It is possible to communicate information to the Exception handler from the point of the exception. That information is the type of thrown object or information placed into the thrown object. 16. Exceptions are caught by the closest exception handler (for the try block from which the exception was thrown) specifying an appropriate type. 17. Errors are not always checked explicitly. A try block may appear to contain no error checking and include no throw statements. But code referenced in the try block could certainly cause error-checking code in constructors to execute. 18. Exception handlers are contained in catch blocks. Each catch block starts with the keyword catch followed by parentheses containing a type and an optional parameter name. This is followed by braces delineating the Exception handling code. When an exception is caught, the code in the catch block is executed. 19. It is possible to specify customized behavior to replace function terminate by designating another function to be executed and providing that function's name as the argument is a set_terminate function call. 20. catch (...) means to catch all exceptions. 21. It is possible that no handler will match a particular thrown object. This causes the search for a match to continue in an enclosing try block. 22. The exception handlers are searched in order for an appropriate match. The first handler that yields a match is executed. When that handler finishes executing, control resumes with the first statement after the last catch block. 23. The order of the exception handlers affects how an exception is handled. 24. An exception handler can not directly access variables in the scope of its try block. Information the handler needs is normally passed in the thrown object. 25. An exception handler catches a derived-class object should be palced before a handler that catches a base-class objects and the object of classes derived from that base class. 26. When an exception is caught, it is possible that resources may have been allocated but not yet released in the try block. The catch handler should release these resources. 27. Even if a handler can process an exception, and regardless of whether it does any processing on that exception, the handler can rethrow the exception for further processing outside the handler. A rethrown exception is detected by the next enclosing try block and is handled by an exception handler listed after that enclosing try block. 28. Prototypes for functions set_terminate ans set_unexpected are found in handler files and , respectively. 29. Function set_terminate and set_unexpected take pointers to functions as arguments. Each argument must point to a function with void return type and no argument. 30. Exception handling is generally implemented in compilers in such a manner that when an exception does not occur, little or no overhead is imposed by the presense of exception handling code. When exceptions happen, they do incur execution-time overhead. Certainly the presence of exception-handling code makes the program comsume more memory.