/****************************************************************************** *** FILE: finally.cpp *** DATE: 03/20/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This file illustrates how the MS C++ __finally block works. *** If you use command line compiler use: *** c:\> cl finally.cpp /clr /EHs ******************************************************************************/ #include using namespace std; void throw_exception_func() { int i; try{ cout<<"What exception you want me to throw?\n" <<" 1 - logic_error (not handled in the function)\n" <<" 2 - out_of_range (handled in the function)\n" <<"Your choice: "; cin>>i; cout<<"TRY block of the throw_exception_func() function.\n Throwing a "; switch( i ){ case 1: cout<<"login error exception ...\n"; throw logic_error("Let's emulate an error"); case 2: cout<<"out_of_range exception ...\n"; throw out_of_range("Let's emulate an error"); default: cout<<"you_can't read exception ...\n"; throw range_error("Let's emulate an error"); } } catch(out_of_range &e){ cerr<<"out_of_range detected in the throw_exception_func() function. Rethrowing it.\n"; throw; } __finally{ cout<<"We are in the __finally block. Let's clean up.\n"; } } void main() { try{ throw_exception_func(); } catch(logic_error &e){ cerr<<"\nlogic error is detected in the main()\n"; } }