Thread overview | |||||
---|---|---|---|---|---|
|
July 11, 2011 Is there any way I can have one handler for multiple exceptions? | ||||
---|---|---|---|---|
| ||||
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? My use case is for the cmdln.interact library, I'm using userInput!() to get an integer from the user and the function can throw either a NoInputException or ConvException, but I don't really care which one it is because if either exception is thrown my next action is to use a predefined default. Their base class is Exception, but catching all forms of Exceptions is a bad idea. |
July 12, 2011 Re: Is there any way I can have one handler for multiple exceptions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Mon, 11 Jul 2011 18:08:19 -0400, Andrej Mitrovic <andrej.mitrovich@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
|
July 12, 2011 Re: Is there any way I can have one handler for multiple exceptions? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 7/12/11, Steven Schveighoffer <schveiguy@yahoo.com> wrote: > Call a function? > void main() > { > void handleEx(Exception e) > { > // exception handling code here > } > > try > { > } > catch(Foo f) > { > handleEx(f); > } > catch(Bar b) > { > handleEx(b); > } > } Yeah that's what I did finally. > > Another option is *shudders* goto. > > -Steve > Get outta here! :p |
Copyright © 1999-2021 by the D Language Foundation