C++ Logo

std-proposals

Advanced search

[std-proposals] Common code for all exceptions thrown

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 11 Jan 2024 14:42:33 +0000
Today I wrote the following code; look at how I use a stack variable
"exception_was_thrown":

    bool exception_was_thrown = false;

    try
    {
            Entry_Point_Thread_Terminal_Proper(str_comport);
            rs232win.Close(); // This is harmless no-op if the port isn't open
    }
    catch(std::exception const &e)
    {
        exception_was_thrown = true;
        LogText( std::string( "Exception (" ) + e.what() + ")" );
    }
    catch(...)
    {
        exception_was_thrown = true;
        LogText("Unknown Exception");
    }

    if ( exception_was_thrown )
    {
        pdialog->CallAfter(&Dialog_Main::CallTerminateFullRun);
    }

Upon the catching of an exception, I want some common code to be
executed for all of the 'catch' clauses. What if we could do the
following?

    try
    {
            Entry_Point_Thread_Terminal_Proper(str_comport);
            rs232win.Close(); // This is harmless no-op if the port isn't open
    }
    catch(std::exception const &e)
    {
        LogText( std::string( "Exception (" ) + e.what() + ")" );
    }
    catch(...)
    {
        LogText("Unknown Exception");
    }
    ~catch
    {
        pdialog->CallAfter( &Dialog_Main::CallTerminateFullRun );
    }

In the above snippet, I use "~catch" to indicate that the code is
executed AFTER the exception has been handled by one of the above
'catch' clauses. Perhaps we could use 'catch' by itself to indicate
that the code should be executed BEFORE, as follows:

    try
    {
            Entry_Point_Thread_Terminal_Proper(str_comport);
            rs232win.Close(); // This is harmless no-op if the port isn't open
    }
    catch
    {
        pdialog->CallAfter( &Dialog_Main::CallTerminateFullRun );
    }
    catch(std::exception const &e)
    {
        LogText( std::string( "Exception (" ) + e.what() + ")" );
    }
    catch(...)
    {
        LogText("Unknown Exception");
    }

And then maybe even be able to combine them:

    try
    {
            Entry_Point_Thread_Terminal_Proper(str_comport);
            rs232win.Close(); // This is harmless no-op if the port isn't open
    }
    catch
    {
        pdialog->CallAfter( &Dialog_Main::CallTerminateFullRun );
    }
    catch(std::exception const &e)
    {
        LogText( std::string( "Exception (" ) + e.what() + ")" );
    }
    catch(...)
    {
        LogText("Unknown Exception");
    }
    ~catch
    {
        pdialog->CallAfter( &Dialog_Main::OnTheWayOut );
    }

The above code snippet will be treated by the compiler exactly as if
you had written:

    try
    {
            Entry_Point_Thread_Terminal_Proper(str_comport);
            rs232win.Close(); // This is harmless no-op if the port isn't open
    }
    catch(std::exception const &e)
    {
        pdialog->CallAfter( &Dialog_Main::CallTerminateFullRun );
        LogText( std::string( "Exception (" ) + e.what() + ")" );
        pdialog->CallAfter( &Dialog_Main::OnTheWayOut );
    }
    catch(...)
    {
        pdialog->CallAfter( &Dialog_Main::CallTerminateFullRun );
        LogText("Unknown Exception");
        pdialog->CallAfter( &Dialog_Main::OnTheWayOut );
    }

Received on 2024-01-11 14:42:43