question about "Error Handling"

Laith Scofield

New member
Joined
Oct 24, 2010
Messages
3
Programming Experience
3-5
Hi everybody
I got a question about "Error Handling" in VB.net
what's the meaning of "Throw Exception" and "Throw new Exception" ??
and When we should use it ?
I understand the using of Try...Catch...Finally
But I don't know the using of "Throw" ??
please help...
 
Wow, huge font, +1 for the hard to read

Throwing exceptions is for when you want to notify something outside of your method/class that something went wrong or something can't be done in which case whoever's using it will need to have a Try/Catch block to catch your exception thrown.

There's three ways to get an exception to throw:
Throw an exception that's you get from a Try/catch:
VB.NET:
Try
  'Some code that might cause an exception
Catch ex As Exception
  'Rethrow it:
  Throw ex
End Try

Or you could declare some exception of your own:
VB.NET:
If SomeCondition Then
  Dim SomeException As New Exception(....)
  Throw SomeException
End If
Or you could declare and throw it in the same line:
VB.NET:
If SomeCondition Then
  Throw New Exception(...)
End If
 
JuggaloBrotha said:
'Rethrow it:
Throw ex
This actually makes it harder to debug, because you lose the stacktrace. You can use an empty Throw to rethrow and preserve stacktrace (adding re-thrower to it), or just not catch the exception and let it bubble. See Throw Statement (Visual Basic)
 
Back
Top