Guidelines For Exception Handling
1. It is not essential for the throw statement to appear in the try block in order to throw an exception. The throw statement can be placed in any function, if the function is to be invoked through the try block.
Fig. 19.3 throw statement out of try block
As shown in Figure 19.3, the throw statement is present in the show() function. The show() function is invoked inside the try block. Thus, exception handling can also be defined in the manner described above.
2. When an exception that is not specified is thrown, it is known as an unexpected exception.
3. In case an exception is thrown before the complete execution of a constructor, the destructor for that object will not be executed.
4. As soon as an exception is thrown, the compiler searches nearby handlers (catch blocks). After finding a match, it will be executed.
5. Overuse of exception handling increases the program size. So, apply it whenever most necessary. Incorrect use of exception handling is not consistent and generates bugs in the program. It is hard to debug such bugs.