Bug 7540 – Catch multiple exception types with single catch block

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-18T23:47:51Z
Last change time
2020-08-26T15:07:53Z
Assigned to
No Owner
Creator
yebblies

Comments

Comment #0 by yebblies — 2012-02-18T23:47:51Z
In some cases, multiple exceptions require the exact same handling code in a catch block, but are not the only children of a common base class. class MyExceptionA : Exception {} class MyExceptionB : Exception {} class MyExceptionC : Exception {} void main() { try { trySomething(); } catch (e : MyExceptionA, MyExceptionB) { // handle A and B } catch (MyExceptionC e) { // handle C a different way } } This avoid the current messy solutions such as duplicating the bodies, using goto (does this even work?), moving the code to a function, or catching the base class and using typeinfo to choose.
Comment #1 by yebblies — 2012-02-19T04:48:38Z
Comment #2 by yebblies — 2014-09-08T10:47:31Z
Andrei has requested a variant of this (see pull request for details) that I'm not in favor of, so I'm going to unassign myself.
Comment #3 by pro.mathias.lang — 2020-08-26T15:07:53Z
I'm going to close this as WONTFIX. The issue has been open for 8 1/2 year, and I think if a PR was raised for it today, it would create quite some controversy, with probably as much people in support as against. In other words, this is DIP material.