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

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Fri Mar 27 01:02:11 PDT 2015


On 2015-03-26 12:23, Andrej Mitrovic via Digitalmars-d wrote:
> 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.

Sounds like you want a catch statement without being tied to a try 
statement. Something like this exists in Ruby:

With begin/end:

def foo
   begin
     raise 'foo'
   rescue Exception => E
     p e
   end
end

Without:

def foo
   raise 'foo'
rescue Exception => E // tied to the function scope
   p e
end

In the above example the "rescue" is tied to the function scope. 
Something similar could be supported in D:

void foo ()
{
   throw new Exception("foo");

   catch (Exception e) // tied to the function scope
     writeln(e);
}

Or possibly tie it to the most enclosing scope:


void foo ()
{
   {
     throw new Exception("foo");

     catch (Exception e) // tied to the scope
       writeln(e);
   }
}

I wouldn't mind if this was supported.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list