20

There is sometimes that one want to do the same on two different types of Exception. I searched, but I didn't find anything for VB.NET.

Simple example:

Try
    '...
Catch ex As Net.WebException
    'Do something
Catch ex As Net.CookieException
    'Do the same
Catch ex As Exception
    '...
End Try

I wonder if there is a way to catch both exceptions at once without needed to repeat code.

2

1 Answer 1

36

As seen on Catch multiple exceptions at once? it can be done this way:

Catch ex As Exception When TypeOf ex Is FormatException OrElse TypeOf ex Is OverflowException
Sign up to request clarification or add additional context in comments.

2 Comments

In this situation I would have offered ALvin a chance to write his comment as an answer and then accept that, rather than writing it to an answer myself.
If you are wanting to catch an exception that derives from Exception, you must place it before any that are "plain vanilla" exceptions. I just tested this with FaultException on a SOAP service call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.