![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The first three statements inside the main function are integer definitions used for calculations later in the source. Next, you find the first occurrence of exception-handling code. That section of code follows: try { result = numerator / divisor ; cout << \nExpression succeeded! << endl ; } catch( ... ) { cout << \nDivide-by-zero exception! << endl ; } In a nutshell, the program attempts to execute the statement within the try block; if it is unsuccessful, control is transferred to the catch block. Any statements following the statement throwing an exception will be disregarded. If a statement within a try block does not throw an exception, control continues on to the next statement, if one exists. A try block consists of the keyword try, followed by an opening brace, one or more program statements, and a closing brace. It is assumed that one or more statements within a try block will throw an exception; if they dont, there is no need to use the exception mechanism. A try block is the same as any other C++ block. Variables can be declared within the block, but be forewarned that those variables will not be accessible outside the try block. One or more catch blocks must follow a try block. A catch clause is sometimes referred to as an exception handler. The first catch block in the program is shown here: catch( ... ) { cout << \nDivide-by-zero exception! << endl ; } This is a unique catch clause, commonly referred to as the catch all clause. The syntax for this catch clause consists of an ellipsis (three periods) within the catch declaration. The catch all catch block must be the last one, if more than one catch block is defined. If the preceding try block throws an exception, the cout statement within the catch clause will execute, proclaiming a divide-by-zero exception. The next try/catch in the program is try { for( divisor = 10; divisor >= 0; divisor-- ) { result = numerator / divisor ; cout << numerator << / << divisor ; cout << == << result << endl ; } } catch( ... ) { cout << for() divide-by-zero exception! << endl ; } This for loop is surrounded by a try block. The loop will continue to execute until divisor is less than 0 or if an exception is thrown. Obviously, the loop will never reach 1 because a divide-by-zero exception occurs when divisors value becomes 0. The next try/catch section of code follows: try { acquireMemory( 30000 ) ; } catch( string s ) { cout << s << endl ; } catch( ... ) { cout << Exception thrown for ; cout << acquireMemory() << endl ; } Within this try block is a call to a function named acquireMemory. Obviously, this function potentially throws an exception of some type. Lets jump ahead and see what is happening within the function acquireMemory(). The functions body is shown in the following code: void acquireMemory( int elements ) { long cnt = elements * MAX_ELEMENTS ; LargeStruct *s = (LargeStruct *)0 ; try { s = new LargeStruct[ cnt ] ; } catch( bad_alloc e ) { cout << Caught bad_alloc << endl ; } catch( ... ) { cout << allocation exception << endl ; throw ; } if( s == (LargeStruct *)0 ) throw string( s is null in acquireMemory ) ; else delete [] s ; } The function accepts an integer argument, specifying the number of elements to allocate. The first two statements within the function define a variable that holds the total number of elements to allocate and a declaration of a pointer to LargeStruct, respectively. Next, a try block is encountered, followed by two catch clauses. Within the try block, an attempt is made to allocate an array of LargeStructs. If the allocation fails and new throws a bad_alloc exception, the first catch clause processes the exception. If any other exception is thrown, it will be handled by the catch all handler. Notice the use of throw within the catch all handler; this instructs the handling mechanism to throw the same exception up to the next catch handler.
|
![]() |
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.
|