Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 11 commits into from

Conversation

yebblies
Copy link
Member

Allow catching multiple exceptions with:

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

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

@WalterBright
Copy link
Member

fails autotester

@yebblies
Copy link
Member Author

Conflicted with pull #737, should work now.

{
Type *t = (*types)[j];
if (tn->implicitConvTo(t))
error(loc, "%s is already covered by base class %s", tn->toChars(), t->toChars());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@MartinNowak
Copy link
Member

Looks good to me.

@yebblies
Copy link
Member Author

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

@alexrp
Copy link
Member

alexrp commented Jul 15, 2012

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
Copy link
Member Author

@alexrp That's the idea.

Allow catching multiple exceptions with 'catch(auto e : E1, E2, E3)' syntax.
Expands TypeTuples in the type list.
@andralex
Copy link
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
Copy link
Member Author

@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
Copy link
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
Copy link
Member Author

yebblies commented May 7, 2013

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants