D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7540 - Catch multiple exception types with single catch block
Summary: Catch multiple exception types with single catch block
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-18 23:47 UTC by yebblies
Modified: 2020-08-26 15:07 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description yebblies 2012-02-18 23:47:51 UTC
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 2 yebblies 2014-09-08 10:47:31 UTC
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 Mathias LANG 2020-08-26 15:07:53 UTC
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.