views:

41

answers:

5

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

+1  A: 

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
Jon Skeet
A: 

You first have to check err.InnerException is null or not and than go for string message comparison

Pranay Rana
+2  A: 

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
David
Where do I need to put this? Thanks
Jamie Taylor
@Jamie Taylor - there is a difference in @Davids sample and what you have in your question. Are you sure you want *the innermost* exception?
Peter Lillevold
@Peter Lillevold - I want to get the innermost exception if there is one and if there isn't one then I just want to get the primary exception
Jamie Taylor
A: 

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
SWeko
I get an Error Type List is not defined. Any ideas?
Jamie Taylor
My VB is a bit rusty, modified the code to at least compile.
SWeko
+1  A: 

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
Peter Lillevold