19

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.

SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • 3
    The same way as you do in C#, only the syntax would be a little bit different. http://stackoverflow.com/questions/136035/catch-multiple-exceptions-at-once – Alvin Wong Feb 15 '13 at 06:46
  • 6
    Hey wait... the second answer has exactly what you want. http://stackoverflow.com/a/136114/1386111 – Alvin Wong Feb 15 '13 at 06:49

1 Answers1

35

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
Community
  • 1
  • 1
SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • 9
    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. – Pezzzz Feb 15 '13 at 10:03
  • 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. – jinzai Jul 30 '18 at 22:21