More Elegant Exception Handling Than Multiple Catch Blocks

More Elegant Exception Handling Than Multiple Catch Blocks?

In my opinion, a bunch of "ugly" catch blocks IS the best way to handle that situation.

The reason I prefer this is that it is very explicit. You are explicitly stating which exceptions you want to handle, and how they should be handled. Other forms of trying to merge handling into more concise forms lose readability in most cases.

My advice would be to stick to this, and handle the exceptions you wish to handle explicitly, each in their own catch block.

try- catch. Handling multiple exceptions the same way (or with a fall through)

Currently there is no language construct to accomplish what you want. Unless the exception all derive from a base exception you need to consider refactoring the common logic to a method and call it from the different exception handlers.

Alternatively you could do as explained in this question:

Catch multiple Exceptions at once?

Personally I tend to prefer the method-based approach.

Using multi-catch Exception type in constructor

Of course that is not a valid constructor :

Wrapper(Api1Ex | Api2Ex e){
mTrigger = e;
}

In your last sample, the creation of an exception that may trigger the throw of another exception if the type of the original exception doesn't match is not the correct way either (while it compiles).

The best way to bound the parameter type of the constructor is using a common base type for the both exceptions and specify it as parameter such as :

private final GenericApiException genericApiException;
WrapperException(GenericApiException genericApiException){
super(genericApiException);
this.genericApiException = genericApiException;
}

You can so use that constructor :

catch (Api1Ex | Api2ex e){
throw new WrapperException(e);
]

WrapperException looks clearer than Wrapper.


the big but is that I cannot modify api1 or 2 in any way or form

In this case, overload the constructor :

WrapperException(Api1Ex e){
mTrigger = e;
}

WrapperException(Api2Ex e){
mTrigger = e;
}

Can you catch more than one type of exception with each block?

You can use :

catch (SystemException ex)
{
if( (ex is IOException)
|| (ex is UnauthorizedAccessException )
// These are redundant
// || (ex is PathTooLongException )
// || (ex is DirectoryNotFoundException )
|| (ex is NotSupportedException )
)
lblBpsError.Text = ex.Message;
else
throw;
}

Catch multiple exceptions at once?

Catch System.Exception and switch on the types

catch (Exception ex)            
{
if (ex is FormatException || ex is OverflowException)
{
WebId = Guid.Empty;
return;
}

throw;
}

VB.NET Try Catch with multiple Catch blocks

Teejay has the correct answer.

However, if your Catch block is empty it makes no sense at all to handle this exception. You just want to prevent the last block from catching it. You can use your method – but consider that having an empty Catch block is normally inacceptable: exceptions should either not be caught, or should be handled properly; swallowing them silently must be seen as a bug. Your case is an exception to this rule but as such it needs to be documented in code since it will confuse careful maintainers otherwise.

Well, VB has a special idiom for exactly this situation:

Try
' …
Catch ex As Exception When Not TypeOf ex Is ThreadAbortException
' Only executed if `ex` isn’t a ThreadAbortException
End Try

This code doesn’t catch ThreadAbortException at all, which is the right thing to do if you don’t want to handle it: ThreadAbortException cannot be swallowed so even when you catch it, it will be rethrown at the end of the Catch block.

Note that this is fundamentally different from SysDragon’s answer which uses a conventional If statement while the code here uses a special clause in the Catch statement as a filter.

Is there a more elegant way to do error handling than spreading try/catch in the entire app code?

You need to read up on structured exception handling. If you're using as many exception handlers as it sounds then you're doing it wrong.

Exception handling isn't like checking return values. You are supposed to handle some exceptions in limited, key spots in your code not all over the place. Remeber that exceptions "bubble up" the call stack!

Here is a good and well-reviewed CodeProject article on exception best practices.

Thorough use of 'if' statements or 'try/catch' blocks?

this debate (2003) was good:

http://www.joelonsoftware.com/items/2003/10/13.html

http://nedbatchelder.com/blog/200310/joel_on_exceptions.html

How to catch two exceptions of the same type and handle them differently?

Is there some way to check which of the two method calls is the one that threw the exception?

You can do this literally by getting the stack trace and iterating through it to find the method you called. I don't recommend you do this as it's more complicated and easily broken.

Another approach is to set a flag you can use.

void someMethod() {
String method = "none";
try {
method = "doSomething";
doSomething();

method = "doSomethingElse";
doSomethingElse();
} catch (IllegalInputException e) {
handleError(method, e); // don't ignore the exception.
}


Related Topics



Leave a reply



Submit