locked
What's the different between __finally and finally RRS feed

  • Question

  • Like the title,I found both the __finally and finally can be existed in managed C++ code .

    I guess the __finally is the macro in native c++ ,and the finally is the keyword in managed C++

    If yes then I have a question :
    If I have the code like following :
    try
    {
    //do something managed c++ code
    }
    __finally
    {
    }

    does the __finally will avaible ? or we need to change to finally .

    To be code or not . It is a problem.
    Friday, October 9, 2009 8:16 AM

Answers

  • Different languages.  The finally keyword belongs to the C++/CLI language, it must be paired with the try keyword.  The __finally keyword is a Microsoft specific extension to the unmanaged C and C++ language.  It must be paired with the __except keyword.  __except catches SEH exceptions, like access violations, they are specific to Windows.  Standard C++ exceptions do not support a finally block.

    In a managed C++/CLI program you should always use try/catch/finally, never __except.  It catches managed exceptions as well as SEH exceptions.
    Hans Passant.
    • Marked as answer by ms44cn Monday, October 12, 2009 3:21 AM
    Saturday, October 10, 2009 7:48 PM

All replies

  • Different languages.  The finally keyword belongs to the C++/CLI language, it must be paired with the try keyword.  The __finally keyword is a Microsoft specific extension to the unmanaged C and C++ language.  It must be paired with the __except keyword.  __except catches SEH exceptions, like access violations, they are specific to Windows.  Standard C++ exceptions do not support a finally block.

    In a managed C++/CLI program you should always use try/catch/finally, never __except.  It catches managed exceptions as well as SEH exceptions.
    Hans Passant.
    • Marked as answer by ms44cn Monday, October 12, 2009 3:21 AM
    Saturday, October 10, 2009 7:48 PM
  • Further confusing the issue is that a few C++/CLI constructs such as 'finally' can also be used within an unmanaged class in Visual C++.

    e.g., the following compiles fine in Visual C++:
    class Unmanaged
    {
        void test()
        {
            try
            {
                //...
            }
            finally
            {
                //...
            }
        }
    };
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, October 11, 2009 2:51 PM
  • No, the finally keyword is only recognized for code compiled to IL:

    #pragma managed(push, off)
    class Unmanaged {
    public:
      void test() {
        try {}
        finally {}   // Error C3273
      }
    };
    #pragma managed(pop)


    Hans Passant.
    Sunday, October 11, 2009 3:12 PM
  • But without the #pragma's, "class" without "ref" is still an unmanaged class...

    I get the same error when I use your #pragma's, but I think in both cases the class is an unmanaged class (otherwise, why is the "ref" keyword ever used?).

    I think Visual C++ is just replacing "finally" with "__finally" for unmanaged classes.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Sunday, October 11, 2009 3:36 PM
  • It doesn't matter how the class object is allocated and where it is stored.  What matters is how the code is generated.  With /clr in effect, the compiler will generate IL that gets translated to machine code by the JIT compiler at runtime.  Just like regular managed code.  IL supports the finally keyword.  The only way to get the compiler to generate machine code directly, instead of IL, is with #pragma managed.  When it generates machine code, it no longer likes finally.

    There are only a few unmanaged code constructs that cannot be translated to IL.  The __fastcall calling convention is one.

    Hans Passant.
    Sunday, October 11, 2009 4:22 PM