SysDragon
SysDragon

Reputation: 9878

Catch multiple exceptions at the same time

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.

Upvotes: 20

Views: 22386

Answers (1)

SysDragon
SysDragon

Reputation: 9878

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

Upvotes: 36

Related Questions