|
![]() |
|||
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works Begin your exploration of unexpected exceptions at the top of this source file. The sixth line of code provides a typedef to the declaration of the error handler as shown here: typedef void(*pUnExp)() ; This typedef says that pUnExp is a pointer to a function taking no arguments and returning nothing. The next block of code declares a class named ErrorClass. It is used as an exception class to process exceptions. ErrorClass contains a default constructor and a member function message() to print out a diagnostic message. The next line of code is a function declaration for func() as shown: void func( void ) throw( ErrorClass ) ; This function declaration also includes an exception specification. The exception specification states that func() will throw an exception of type ErrorClass. Next, you come to the function definition unexpectedHandler(). This function is used to process unexpected exceptions. The function contains two statements: cout and exit(). You should provide appropriate functionality required to process unexpected exceptions; this code provides a basic example. The first line of code in the main function is a call to the set_terminate() library function. This call will set the function unexpectedHandler() to handle unexpected exceptions. The return from set_terminate() is the address of the most current handler. The next block of code consists of a try-catch code section. A call to func() is made within the try block. The only handler defined consists of the catch clause accepting an exception object of type ErrorClass. Lets turn our attention to the definition of func(). The definition follows: void func( ) throw( ErrorClass ) { throw( double(10.0) ) ; } Notice that the exception specification specifies that this function will only throw exceptions of type ErrorClass. But hold on a minutewhat do you see in the body of the function? The throw expression throws a double. However, there is no need to worry because the application is prepared to process unexpected exceptions as the following shows: Unexpected handler! You might have expected to see the following message: ErrorClass This is the message displayed from the ErrorClasss member function message(). Indeed, the catch clause in the main function contains the call: e.message(). Because the exception thrown is not of type ErrorClass, this catch clause never gets the opportunity to handle the exception. Instead, the exception handling mechanism looks up the address of the current unexpected handler and invokes the function unexpectedHandler(), which is defined within this application. Comments Considering most applications these days exceed 100,000 lines of code, it is imperative that you take a defensive stance when designing and developing applications. Exception handling is a feature you should be utilizing. Unexpected exceptions can take you by surprise if your application is not prepared to handle the unexpected. This How-To introduced you to handling unexpected exceptions. With this knowledge, you are in a better position to express your defensive programming position.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|