Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 11 commits into from

5 participants

@yebblies
Collaborator

Allow catching multiple exceptions with:

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

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

@WalterBright

fails autotester

@yebblies
Collaborator

Conflicted with pull #737, should work now.

src/statement.c
((42 lines not shown))
+ !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 Collaborator

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

@yebblies Collaborator

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 Collaborator

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
Collaborator

Looks good to me.

@yebblies
Collaborator

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

@alexrp

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
Collaborator

@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
Owner

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
Collaborator

@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
Owner

@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
Collaborator

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

@yebblies yebblies closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 9, 2012
  1. @WalterBright

    reduce dependence on OMF

    WalterBright authored
  2. @WalterBright

    Merge pull request #387 from 9rnsr/constApply

    WalterBright authored
    Supplemental changes of druntime/pull/72 - Issue 1824 - Object not const correct
Commits on Jul 10, 2012
  1. @WalterBright

    don't set Sseg here

    WalterBright authored
  2. @WalterBright
  3. @WalterBright
Commits on Jul 11, 2012
  1. @WalterBright

    start separating out obj

    WalterBright authored
Commits on Jul 12, 2012
  1. @WalterBright
  2. @WalterBright

    forgot to check this in

    WalterBright authored
  3. @WalterBright

    durn, forgot elfobj

    WalterBright authored
Commits on Jul 13, 2012
  1. @WalterBright

    more obj refactoring

    WalterBright authored
Commits on Jul 16, 2012
  1. @yebblies

    Fix Issue 7540 - Catch multiple exception types with single catch block

    yebblies authored
    Allow catching multiple exceptions with 'catch(auto e : E1, E2, E3)' syntax.
    Expands TypeTuples in the type list.
Something went wrong with that request. Please try again.