2009-11-04 14 views
1

在一個aspx頁面我這樣做:問題在ASP.NET拋出一個錯誤,並且抓住它在Global.asax

Try 

    Throw new IndexOutOfRangeException 

Catch ex As Exception 

    Dim myException As New bizException(ex) 

    Throw myException 

End Try 

在Global.asax我這樣做:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 

    Dim myException As bizException = DirectCast(Server.GetLastError().GetBaseException(), bizException) 


End Sub 

的該錯誤鑄造期間存在的:

InvalidCastException的: 無法轉換類型 'System.IndexOutOfRangeException' 的目的爲類型 'bizException'。

GetLastError的類型是IndexOutOfRangeException而不是bizException ...爲什麼?

回答

0

試試這個:

Dim myException As bizException = DirectCast(Server.GetLastError(), bizException) 

(刪除了 「GetBaseException」 呼叫)

+0

daughtkom: 我有一個不同的錯誤消息: 無法轉換類型「System.Web.HttpUnhandledException」的目的爲類型「bizException」。 – David 2009-11-04 17:12:15

1

在 「的Application_Error」 例行,Server.GetLastError()返回一個類型 'System.Web.HttpUnhandledException' 例外因爲你之前沒有處理錯誤 - 即在方法或頁面級別。

您需要從Server.GetLastError()返回的異常中檢查「InnerException」的內容。

「InnerException」將包含您的「bizException」。

Dim myException As bizException = DirectCast(Server.GetLastError().InnerException, bizException)

不知道這裏的VB.NET語法 - 更多的是C#的人自己。

相關問題