Is there any way I can have one handler for multiple exceptions?

Steven Schveighoffer schveiguy at yahoo.com
Tue Jul 12 05:16:13 PDT 2011


On Mon, 11 Jul 2011 18:08:19 -0400, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> I'm trying to do something like the following:
>
> import std.exception;
>
> class Foo : Exception
> {
>     this(string msg) { super(msg); }
> }
>
> class Bar : Exception
> {
>     this(string msg) { super(msg); }
> }
>
> void main()
> {
>     try
>     {
>     }
>     catch (Foo) catch (Bar)
>     {
>     }
> }
>
> Some function might throw two types of exceptions, but I don't care
> which one it is, I'd like to handle them both within one catch block.
> Is this possible?

Call a function?


void main()
{
    void handleEx(Exception e)
    {
       // exception handling code here
    }

    try
    {
    }
    catch(Foo f)
    {
       handleEx(f);
    }
    catch(Bar b)
    {
       handleEx(b);
    }
}

Another option is *shudders* goto.

-Steve


More information about the Digitalmars-d-learn mailing list