Rethrow an exception like in C++?

Rob T alanb at ucora.com
Fri Mar 8 12:42:54 PST 2013


On Friday, 8 March 2013 at 20:32:23 UTC, Jonathan M Davis wrote:
> On Friday, March 08, 2013 20:16:13 Rob T wrote:
>> If you know of a better way to implement an exception handler 
>> in
>> D, then I'd like to know about it. For example I do know that 
>> D's
>> system allows you to insert callback functions, but I don't yet
>> know how to make use out of it, so perhaps there's a better 
>> way.
>
> If all you need to do is do something when an exception is 
> thrown and don't
> need the exception itself, then just use scope statements:
>
> http://dlang.org/statement.html#ScopeGuardStatement
>
> scope(failure) doSomethingOnException();
> //code that might throw
>
> The block inside the scope statement will run when the 
> surrounding scope is
> exited with an exception. That _really_ cleans up a lot of code 
> that just
> wants to react to the fact that an exception was thrown and not 
> actually do
> anything with it.
>
> - Jonathan M Davis

Yes, scope(...) does seem useful in certain situations.

In my case I need access to the information contained in the 
Exception object so that I can log what kind of error occurred 
and also whatever other details there are, and then perform a few 
general operations such as possibly sending out a notification to 
support staff.

I am thinking that perhaps a callback may do what I want, so 
that's something I intend to look at more closely.

--rt


More information about the Digitalmars-d-learn mailing list