Rethrow an exception like in C++?

Rob T alanb at ucora.com
Fri Mar 8 09:32:43 PST 2013


On Friday, 8 March 2013 at 09:07:59 UTC, Chris Cain wrote:
> On Friday, 8 March 2013 at 09:01:14 UTC, Jonathan M Davis wrote:
>> If that's the case, I really don't see what the problem is. 
>> It's just a few
>> characters difference.
>
> To be honest, the way I look at it, D does this _better_. 
> "throw;" looks like magic and it's accessing global 
> (potentially mutable) state to do something. I haven't done 
> much research into C++'s handling of exceptions (and, in 
> particular, this feature), but I'd be _very_ weary of doing 
> that without knowing whether this global state is thread local 
> or shared. If it's shared, you would have some nice heisenbugs 
> pop up if this was used in multithreaded code.
>
> Using "catch(Exception E) { throw Trace(E); }" is more explicit 
> to what Trace needs to work and, IMO, a better design.

The point I'm trying to illustrate is difficult to understand 
unless you have to personally implement 1000 try/catch statements 
in your code base. You'll never know how a seemingly trivial item 
like re-specifying the same pointless catch statement over and 
over and over again quickly adds up and the frustration knowing 
that there's a simple solution at hand, but it's not available in 
what is otherwise a much better language than the one that has 
the solution.

I'm not at all trying to make a case that D's exception handling 
is somehow inferior to C++. What tends to happen with D is that 
it's very close to being "really really good" but it is not yet 
refined to the same degree that a mature language is, so there 
are always a few items here and there that could be made to work 
better. Pointing them out is not an attack on D, it's what is 
necessary to improve the language.

I suspect it is simple to implement a safe re-throw in D, it's 
just hard to see why you would want to use it unless you have the 
experience of making use of try catch very frequently in a large 
code base.

--rt


More information about the Digitalmars-d-learn mailing list