Rethrow an exception like in C++?

Jonathan M Davis jmdavisProg at gmx.com
Fri Mar 8 15:21:13 PST 2013


On Friday, March 08, 2013 20:16:13 Rob T wrote:
> On Friday, 8 March 2013 at 18:49:53 UTC, Jonathan M Davis wrote:
> > Except that the C++ one is just as pointless. In both cases,
> > you're telling it
> > to catch everything. It's just that syntax is slightly
> > different, because D
> > doesn't allow you to throw without an explicit variable. And
> > it's only a
> > handful of characters difference in length. So, to some of us
> > at least, it
> > seems like you're blowing things out of proportion. And given
> > the lack of
> > clarity in the C++ solution, it comes off as being worse from a
> > technical
> > perspective, regardless of the typing involved.
> > 
> > - Jonathan M Davis
> 
> I don't wish to blow things out of proportion, and will say again
> that the main objective I was trying to achieve has been met,
> thanks to the assistance I received in here, so this remaining
> item is not all that major, it's just an unnecessary repetitive
> nuisance to me.
> 
> From a technical stand point, I'm implementing a reusable
> exception handler or dispatcher (I've seen these two terms used
> to describe it) which is extremely useful to me, and I would
> assume to many others. There are examples of this concept
> implemented in C++ by other programmers, that's how I got the
> idea.
> 
> Before I started using an exception handler, my exception
> handling was very limited and tedious to implement, and I never
> saw a need to re-throw an exception. There may be other uses for
> rethrow that I'm not aware of.
> 
> What my C++ exception handler does not require, is the exception
> reference passed in as an argument, so that's one of the main
> differences between what D allows and what C++ allows. In my case
> every catch statement and function call will be identical, it's
> slightly more tedious to type in than the C++ version. No big
> deal for you, but it is annoying for me because I use this form
> very frequently.
> 
> 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.
> 
> Any further help or insight on this matter is appreciated.

Well, ultimately, it looks like the only difference between C++ and D on this, 
would be that

std::exception Trace()
{
 try
 {
 // key item missing from D
 throw; // <= rethrow last exception
 }
 catch( EType1 &E )
 {
 // do stuff
 return new std::exception( ...
 }
 catch( EType2 &E )
 {
 // do different stuff
 return new std::exception( ...
 }
 catch(...)
 {
 // unkown type, do something else
 throw new std::exception( ...
 }

}

would become

Exception Trace(Exception e)
{
 try
 {
 // key item missing from D
 throw e; // <= rethrow last exception
 }
 catch( EType1 &e )
 {
 // do stuff
 return new std::exception( ...
 }
 catch( EType2 &e )
 {
 // do different stuff
 return new std::exception( ...
 }
 catch(...)
 {
 // unkown type, do something else
 throw new std::exception( ...
 }

}

or this

Exception Trace(Exception e)
{
 if(cast(Etype1)e)
 {
 // do stuff
 return new Exception( ...
 }
 else if(cast(Etype2)e)
 {
 // do different stuff
 return new Exception( ...
 }
 else
 {
 // unkown type, do something else
 throw new Exception( ...
 }
}

And in the caller,

try {...}
catch(...)
{
 throw Trace();
}

would become

try {...}
catch(Exception e)
{
 throw Trace(e);
}

So, ultimately, the code is nearly identical, and if you use casts, you can 
actually make Trace shorter and more efficient (since it avoids the extra 
throw). And so while D may not improve on the C++ code much, it seems to me 
that you can do pretty much exactly the same thing in both. There are 
differences, but they're quite minimal.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list