RAD Studio
ContentsIndex
PreviousUpNext
__finally

Category 

Statements, Keyword extensions 

Syntax  

__finally {compound-statement}

Description 

The __finally keyword specifies actions that should be taken regardless of how the flow within the preceding __try exits. 

The following is the code fragment shows how to use the try/__finally construct:

#include <stdio.h>#include <string.h>#include <windows.h>class Exception{public:Exception(char* s = "Unknown"){what = strdup(s);      }Exception(const Exception& e ){what = strdup(e.what); } ~Exception()                   {free(what);         }  char* msg() const             {return what;           }private:char* what;};int main(){float e, f, g;try  {try    {f = 1.0;g = 0.0;try      {puts("Another exception:");e = f / g;      }__except(EXCEPTION_EXECUTE_HANDLER)      {puts("Caught a C-based exception.");throw(Exception("Hardware error: Divide by 0"));      }    }catch(const Exception& e)    {printf("Caught C++ Exception: %s :\n", e.msg());    }  }__finally  {puts("C++ allows __finally too!");  }return e;}
#include <string.h>
#include <windows.h>
class Exception
{
public:
Exception(char* s = "Unknown"){what = strdup(s);      }
Exception(const Exception& e ){what = strdup(e.what); }
 ~Exception()                   {free(what);         }
  char* msg() const             {return what;           }
private:
char* what;
};
int main()
{
float e, f, g;
try
  {
try
    {
f = 1.0;
g = 0.0;
try
      {
puts("Another exception:");
e = f / g;
      }
__except(EXCEPTION_EXECUTE_HANDLER)
      {
puts("Caught a C-based exception.");
throw(Exception("Hardware error: Divide by 0"));
      }
    }
catch(const Exception& e)
    {
printf("Caught C++ Exception: %s :\n", e.msg());
    }
  }
__finally
  {
puts("C++ allows __finally too!");
  }
return e;
}

 

#include <iostream>#include <stdexcept>using namespace std;class MyException : public exception {public:virtual const char *what() const throw() {return("MyException occurred.");}};// Give me any integer...void myFunc(int a){ MyException e;   // ...but not that one.   if(a == 0)     throw(e);}void main(){   int g;   // Note __finally must be in its own try block (with no preceding catch).   try {     try {       g = 0;       myFunc(g);     }     catch(MyException &e) {       cout << "Exception: " << e.what() << endl;     }   }   __finally {       cout << "Finally block reached." << endl;   }
#include <stdexcept>
using namespace std;
class MyException : public exception {
public:
virtual const char *what() const throw() {
return("MyException occurred.");
}
};
// Give me any integer...
void myFunc(int a)
{
 MyException e;
   // ...but not that one.
   if(a == 0)
     throw(e);
}
void main()
{
   int g;
   // Note __finally must be in its own try block (with no preceding catch).
   try {
     try {
       g = 0;
       myFunc(g);
     }
     catch(MyException &e) {
       cout << "Exception: " << e.what() << endl;
     }
   }
   __finally {
       cout << "Finally block reached." << endl;
   }
}

Running the above program results in the following:

Another exception:Caught a C-based exception.Caught C++ exception[Hardware error: Divide by 0]C++ allows __finally too!Exception: MyException occurred.Finally block reached.
Caught a C-based exception.Caught C++ exception[Hardware error: Divide by 0]C++ allows __finally too!Exception: MyException occurred.Finally block reached.
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!