Reputation: 3530
I need to show the InnerException message if there is one and if not then i need to show the Message of the primary exception
Here's what i've got
Dim err As System.Exception = Server.GetLastError
If err.InnerException.Message = "" Then
Dim ErrorDetails As String = err.Message
Else
Dim ErrorDetails As String = err.InnerException.Message
End If
But i'm getting errors
Any ideas?
Thanks
Jamie
Upvotes: 0
Views: 614
Reputation: 8926
this will get you the innermost exceptions message
while err is not nothing
ErrorDetails = err.message
err = err.innerexception
end while
EDIT-
Make it look like this:
Dim err As System.Exception = Server.GetLastError
Dim ErrorDetails As String
while err is not nothing
ErrorDetails = err.message
err = err.innerexception
end while
Upvotes: 2
Reputation: 30902
You might also like to get all the messages between the first and last exception. If you have a deep stack, usually the first exception is too general, and the last is too specific, something along these lines:
Dim ErrorDetails As List(Of String) = New List(Of String)
Dim err As Exception = Server.GetLastError
While Not err Is Nothing
ErrorDetails.Add(err.Message)
err = err.InnerException
End While
Upvotes: 0
Reputation: 33930
You should probably declare your variable in the outer scope, and also check for Nothing instead of empty message string.
Dim ErrorDetails As String
Dim err As System.Exception = Server.GetLastError
If err.InnerException Is Nothing Then
ErrorDetails = err.Message
Else
ErrorDetails = err.InnerException.Message
End If
Upvotes: 1
Reputation: 1500675
If there's no inner exception, then evaluating err.InnerException.Message
is going to throw an exception. You need
If Not err.InnerException Is Nothing
Upvotes: 1