C++ vs C#, Different behavior for Exception in destructor

The following C++ code causes an abnormal program termination. Generally speaking, the program is terminated if an exception is thrown while stack unwind is in process due to another exception. Although process termination is very radical solution, it does so for a good reason. Should it ignore the first exception and continue on the second exception? Should it ignore the second exception and continue on the first exception? Either way, we loose one of two exception. The basic idea is that "crash" is better than "wrong behavior" because we can catch the bug sooner.

#include <iostream>
using namespace std;
struct Foo {
    ~Foo(){
        throw std::logic_error("Exception In Destructor");
    }
};

int main(int argc, char** argv)
{
    try{
        Foo foo;
        throw std::runtime_error("Original Exception");
    }catch(std::runtime_error& e){
        cout << e.what() << endl;
    }
}

In the case of C#, the language designer seemed to have a different opinion. Consider this program:

class Foo : IDisposable
{
    public void Dispose()
    {
        throw new Exception( "Second Exception" );
    }
}

class Program
{
    static void Main( string[] args )
    {
        try
        {
            using( Foo foo = new Foo() )
            {
                throw new Exception( "First Exception" );
            }
        }
        catch( Exception e )
        {
            Console.WriteLine( e.Message );
        }
    }
}

The output is "Second Exception" – meaning the first exception is ignored. Here is the theory. The using statement is equivalent to try/finally (http://msdn.microsoft.com/en-us/library/aa664736(VS.71).aspx). Therefore, the try block can be rewritten to:

        try
        {
            Foo foo = new Foo();
            try
            {
                throw new Exception( "First Exception" );
            }
            finally
            {
                foo.Dispose();
            }
        }
        catch( Exception e )
        {
            Console.WriteLine( e.Message );
        }

According go C# language specification,

If the finally block throws another exception, processing of the current exception is terminated. Otherwise, when control reaches the end point of the finally block, processing of the current exception is continued. (http://msdn.microsoft.com/en-us/library/aa664760(VS.71).aspx)

Advertisements

About Moto

Engineer who likes coding
This entry was posted in Tips. Bookmark the permalink.

One Response to C++ vs C#, Different behavior for Exception in destructor

  1. great article, good to know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s