Jamie Taylor
Jamie Taylor

Reputation: 3530

Statement if InnerException is null then don't show

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

Answers (4)

BlackICE
BlackICE

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

SWeko
SWeko

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

Peter Lillevold
Peter Lillevold

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

Jon Skeet
Jon Skeet

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

Related Questions