The Right Approach to Exceptions
Daniel Murphy
yebblies at nospamgmail.com
Sat Feb 18 23:44:30 PST 2012
"Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
news:mailman.582.1329634661.20196.digitalmars-d at puremagic.com...
> 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
I assume you mean the _first_ catch block?
By the looks of it java 7 has this with an awful syntax.
Maybe we should introduce implicit 'catch fallthrough':
void fun()
{
try { something; }
catch (ExceptionA) {} // falls through implicitly
catch (ExpcetionB)
{ // handle both A and B
break;
}
catch (ExceptionC)
{
// handle exception C
break;
}
}
More information about the Digitalmars-d
mailing list