Handle different exceptions of same exception type
Hello all,
Is there a way I can catch and handle two different exceptions within the same exception type? For example:
Code:
Try
' My code
Catch ex As System.Net.WebException
End Try
I have 2 different exceptions that are System.Net.WebException. I want to handle them different ways. Problem is the catch block above catches them both. Is there a way I can determine if which of the two it is and handle them differently?
Thanks,
Strick
Re: Handle different exceptions of same exception type
How do you distinguish them? Use the same distinction for sorting them out in the catch block
Re: Handle different exceptions of same exception type
The try catch block can also take a when clause. You may use this to distinguish:-
vbnet Code:
'
Try
'Throw New ArgumentException("ARE YOU MAD????")
Throw New ArgumentException("YA U ARE MAD!!!")
Catch ex As ArgumentException When ex.Message.Contains("ARE YOU MAD")
MsgBox("Are you mad ?")
Catch ex As ArgumentException When ex.Message.Contains("YA U")
MsgBox("You are mad")
End Try
You can try out the above as see. Comment/Uncomment either one and you will see the relevant catch block handle it.
Re: Handle different exceptions of same exception type
Thanks guys! This is what I was looking for.
Strick