Chapt 15. Exceptions

15.1. Introduction to Exception Handling: (Learn C++)

Exception handling provide more flexible mechanism to decouple error handling from typical control flow of code

15.2. Basic Exception Handling: (Learn C++)

Exception handling in C++ implemented with three parts: throw, try, catch

• throw statement:

act as the signal to indicate that exceptions or errors have occurred

throw keyword + single value of data type(used to identify appropriate handler)

• try block:

act as observer to observe any exceptions or errors occurred in try block

* when throw statement executed, the remaining part of try block must be never reached, and so never executed

• catch block:

act as handler to handle the exceptions or errors with matched signal data type

multiple catch block in sequence possible

∗ if there no appropriate catch handler exist, exception handling delegated to next nearest try-catch block, if no more try-catch block exist the program will fail witch exception errors

15.3. Exceptions, Functions and Stack Unwinding: (Learn C++)

In C++ functions, even if main(), not necessarily handle exceptions thrown by them.

It means, function can delegate the exception handling to the caller of it. When this happen stack(function call stack) unwinding occurred.

If the caller also has no appropriate exception handler, exception recursive delegated to the caller of the caller.

In this way, it can be implemented to handle the exception with different context. i.e. print text error message, display pop up menu, terminate program immediately

15.4. Uncaught exceptions, catch-all handlers: (Learn C++)

• uncaught exception:

If it is happen that exception is not caught by any function, even if by main, the program will be forced to terminate. Then handling the exception is totally dependent on your OS

• catch-all handler: catch(…)

catch-all handler make it possible to catch all exceptions occurred in try block.

To ensure handling the specific exception, it is recommended to put the catch-all handler at last position of try-catch block. * visual studio enforce this constraint

15.5. Exception, Classes and Inheritance: (Learn C++)

• Exception and member functions:

Exception handling, throw-try-catch, can be achieved in member function

* Therefore, errors when occurred in constructing object can be handled by throw-try-catch block. Constructing processed immediately aborted and destructor never executed.

• Exception class:

1. Object of any class type can be thrown as exception parameter

2. Exception class is just a normal class designed specifically to be thrown for dealing with specific context or problem.

* should catch exception class parameter as reference variable for performance

3. std::exception class:

commonly used by C++ standard library classes for exception handling

• Exception Inheritance:

Flow or controlling catch block is sequential.

So, to handle specific derived class, handler for that derived class must be positioned above the base class.

15.6. Exception dangers and Downsides: (Learn C++)

There some issues you should notice before exploiting the exception handling

• Resource Cleaning

if exception occurred before releasing resource i.e. delete pointer, memory issue will happen. So you should implement releasing resource in catch block, or use some functions automatically releasing resource i.e. std::unique_ptr

• Never Throw Exception in Destructor

destructors always called in stack unwinding process. So if you throw the exception in the desturctor, compiler can’t decided whether to continue stack unwinding process or to handle the new exception. The result is to terminate your program immediately

• Exception slow the performance

if function delegate exception handling to caller, then stack unwinding process necessarily need. It will reduce performance of your program. So cautiously exploit exception handling in case of truly exception cases or fatal errors

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s