The Right Approach to Exceptions

Jonathan M Davis jmdavisProg at gmx.com
Sat Feb 18 22:56:25 PST 2012


On Sunday, February 19, 2012 16:59:49 Daniel Murphy wrote:
> "Nick Sabalausky" <a at a.a> wrote in message
> news:jhprac$2aj$1 at digitalmars.com...
> 
> > The only problem I've ever had with them is that there's no templated
> > catch blocks, so you can't handle two different exceptions with the same
> > code without violating DRY or worse: catching the common base type and
> > rethrowing when it's not what you wanted. Toss in templated catch blocks,
> > and I've have no problem at all.
> 
> Do you mean something like this?
> try
> {
>     something();
> }
> catch (e : ThisException, ThatException, OtherException)
> {
>     static assert(is(typeof(e) == CommonType!(ThisException, ThatException,
> OtherException));
> }
> catch (Exception e)
> {
>     // Every other type derived from Exception
> }
> 
> Or do you think the full power to be able to template catch blocks as if
> they were functions would be useful for something?

I think that being able to have a catch block which took multiple exception 
types would be plenty. There are times when it would be very valuable to be 
able to use the same catch block for multiple exceptions without having to 
catch their base type (which would then potentially catch other exceptions 
which you didn't want to catch). So, something like that second catch block 
that you have there would be very valuable.

- Jonathan M Davis


More information about the Digitalmars-d mailing list