2010-09-28 91 views
0

我需要顯示的InnerException消息,如果有一個,如果沒有的話,我需要顯示主要的例外聲明,如果的InnerException爲null,則不會顯示

這裏的消息就是我有

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 

,但我發現錯誤

任何想法?

感謝

傑米

回答

1

你或許應該聲明的變量外的範圍,並檢查了沒有而不是空的消息字符串。

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 
1

如果沒有內部異常,然後評估err.InnerException.Message會拋出異常。您需要

If Not err.InnerException Is Nothing 
2

這將讓你最裏面的異常消息

while err is not nothing 
    ErrorDetails = err.message 
    err = err.innerexception 
end while 

編輯 -

使它看起來像這樣:

Dim err As System.Exception = Server.GetLastError 
Dim ErrorDetails As String 

    while err is not nothing 
     ErrorDetails = err.message 
     err = err.innerexception 
    end while 
+0

我需要把這個放在哪裏?謝謝 – 2010-09-28 12:16:23

+0

@Jamie Taylor - @Davids示例與您的問題有什麼區別。你確定你想要*最內層的*異常嗎? – 2010-09-28 12:25:41

+0

@Peter Lillevold - 我想得到最內層的異常,如果有一個,如果沒有一個然後我只想獲得主要異常 – 2010-09-28 13:31:50

0

您也可能想獲得所有第一個和最後一個異常之間的消息。如果你有一個深籌碼,通常是第一個例外是太一般了,最後就是太具體,沿着這些路線的東西:

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

我得到一個錯誤類型列表沒有定義。有任何想法嗎? – 2010-09-28 12:18:10

+0

我的VB有點生疏,修改代碼至少編譯。 – SWeko 2010-09-28 12:30:32

相關問題