|
![]() |
|||
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The argument variable val is assigned the value 32000, but it is the local copy that gets the new value, not the original object thrown. Therefore, when the throw expression is executed, the original object will be thrown back up the call chain. If you need to modify the original object, you should pass by reference, as the following code snippet shows: catch( int & val ) { val = 32000 ; throw ; } The last catch clause in the rethrowFunc() is called the catch-all catch clause and it looks like this: catch( ... ) { //... } The ellipsis is required; this signature makes it visually apparent as to the functionality of this catch clause. The catch-all catch clause must be the last handler if more than one catch clause exists. The catch-all clause can catch any exception. Dont take this as an invitation to use the catch-all clause after every try block. You have seen how the rethrow expression is used as demonstrated within the rethrowFunc() function. Because every catch clause within this function rethrows its exception, you can return to the main function. Again, you find the same catch clauses after the try block in main, including a catch-all clause. The for loop iterates six times, each iteration in turn realizing a different exception. The output of the program confirms this: Int caught: 5 Float caught: 10.5 Double caught: 21 Char* caught: an exceptional string Other exception caught Other exception caught Notice that the Other exception caught text occurs twice in succession. This text is displayed as a result of the catch-all catch clause. The exception thrown in this case is a divide-by-zero exception. This exception is generated, if you recall, in the default case within the throwFunc() function. Comments In the previous section, I mentioned that you shouldnt blindly apply catch-all catch clauses after every try block. However, there are a couple of good reasons why you might want to use a catch-all clause. One reason is that you might not know all the exceptions that might be thrown by some function call or statement within a try block. Another reason might be that you must release some resource within a function. If some statement generates an exception and you dont have a catch clause to handle it, the catch-all will handle it and you can release the resource there. The following example demonstrates this: void func() { char *buf = new char[ 30000 ] ; try { functionCall( ) ; } catch( char *s ) { // code to handle char* exception } catch( ... ) { delete [] buf ; } } How can you be sure that functionCall() only throws a char* exception? Well, you might know it and then again, you might not. You might be dealing with invalid or out-of-date documentation. The catch-all catch clause ensures that a memory leak does not occur under this circumstance. 11.3 Implement and use an exception class?Problem I have been using exception handling in my applications and find the use of native types to be restrictive and uninformative. Is there a way to use a class as an exception type? And if so, how can I implement such a class type? Technique In this How-To, a simple application is used to demonstrate the use of an exception class. The use of an exception class is preferred over simple types because a class is more expressive. Steps
|
![]() |
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.
|