开发者

Statement if InnerException is null then don't show

开发者 https://www.devze.com 2023-01-17 17:37 出处:网络
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

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.In开发者_高级运维nerException.Message

End If

But i'm getting errors

Any ideas?

Thanks

Jamie


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


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


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消