Feature idea: scope (failure, ExceptionSpecification) for catching exceptions

Andrej Mitrovic via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 26 04:23:25 PDT 2015


One idea I'd like to see is to enhance scope(failure) to allow it to
catch a specific type of exception which would allow us to e.g. log
the exception message and potentially re-throw the exception. All of
this without having to nest our code in try/catch statements.

So instead of having code such as the following:

-----
void test ( )
{
    try
    {
        prepare();
        foo();
        finish();

        try
        {
            prepare();
            bar();
            finish();
        }
        catch (MyException ex)
        {
            logger.log("bar() failed with: " ~ ex.msg);
            throw ex;
        }
    }
    catch (MyException ex)
    {
        logger.log("foo() failed with: " ~ ex.msg);
        throw ex;
    }
}
-----

-----
void test ( )
{
    scope (failure, MyException ex)
    {
        logger.log("foo() failed with: " ~ ex.msg);
        throw ex;
    }

    prepare();
    foo();
    finish();

    scope (failure, MyException ex)
    {
        logger.log("bar() failed with: " ~ ex.msg);
        throw ex;
    }

    prepare();
    bar();
    finish();
}
-----

Granted it's not the best example out there, but I think it has
potential. Thoughts?


More information about the Digitalmars-d mailing list