Enhancement 7540 - Catch multiple exception types with single catch block #738

Closed
wants to merge 11 commits into from

5 participants

@yebblies
D Programming Language member

Allow catching multiple exceptions with:

try { ... }
catch(auto e : E1, E2, E3)
{
}

http://d.puremagic.com/issues/show_bug.cgi?id=7540

@WalterBright
D Programming Language member

fails autotester

@yebblies
D Programming Language member

Conflicted with pull #737, should work now.

@MartinNowak MartinNowak and 1 other commented on an outdated diff Feb 25, 2012
src/statement.c
+ !internalCatch &&
+ cd != ClassDeclaration::exception &&
+ !ClassDeclaration::exception->isBaseOf(cd, NULL) &&
+ sc->func->setUnsafe())
+ {
+ error(loc, "can only catch class objects derived from Exception in @safe code, not '%s'", tn->toChars());
+ tn = Type::terror;
+ }
+ (*types)[i] = tn;
+
+ // Look for redundant types
+ for (size_t j = 0; j < i; j++)
+ {
+ Type *t = (*types)[j];
+ if (tn->implicitConvTo(t))
+ error(loc, "%s is already covered by base class %s", tn->toChars(), t->toChars());
@MartinNowak
D Programming Language member

tn and t could be the same. Maybe we could find a slightly better error message.

@yebblies
D Programming Language member

Maybe "%s is listed more than once in catch list" or something. I don't like the normal wording much either. I hate coming up with error messages.

@MartinNowak
D Programming Language member

How about the one from above. "catch of %s hides catch of %s" and "duplicate catch type %s".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@MartinNowak
D Programming Language member

Looks good to me.

@yebblies
D Programming Language member

Error messages improved.
Tuple expansion.
Now with slightly fewer ICEs!

@alexrp
D Programming Language member

Do I understand this correctly? In your example, will e take on the common base type of E1, E2, and E3 (and error out if none exists (which, actually, shouldn't really happen, since we have Throwable...))?

@yebblies
D Programming Language member

@alexrp That's the idea.

@yebblies yebblies Fix Issue 7540 - Catch multiple exception types with single catch block
Allow catching multiple exceptions with 'catch(auto e : E1, E2, E3)' syntax.
Expands TypeTuples in the type list.
9f9c0b7
@andralex
D Programming Language member

I don't think I'm behind this. There's some loss of type information, and the syntax is "surprising". I'd think a better approach is to define a multi-catch as a template that avoids repetition of source code while still preserving full type information. Even that I'm not sure whether it's a big enough improvement.

That being said, I recall another language (C#?) started supporting that recently.

@yebblies
D Programming Language member

@andralex
The syntax is very easy to change.
Is preserving full type information actually useful? I would expect that if a specific exception type needs to be handled differently it would have it's own catch block.
Could you please elaborate on how the template version would be implemented and used?

@andralex
D Programming Language member

@yebblies Whatever the syntax, the block controlled by catch should behave like a template parameterized on the exception type. Otherwise I feel this feature gives with one hand and takes with the other.

@yebblies
D Programming Language member

I'm going to drop this as I have no plans to fix/maintain it in the near future.

@yebblies yebblies closed this May 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment