Problem in ASP.NET to throw an error and catch it in the global.asax
In an aspx web page I'm doing this:
Try
Throw new IndexOutOfRangeException
Catch ex As Exception
Dim myException As New bizException(ex)
Throw myException
End Try
In the global.asax I'm doing this:
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim myException As bizException = DirectCast(Server.GetLastError().GetBaseException(), bizException)
End Sub
An this error is occuring during the cast:
InvalidCastException: Unable to cast object of type 'System.IndexOutOfRangeException' 开发者_JS百科to type 'bizException'.
The GetLastError's type is IndexOutOfRangeException and not bizException... Why ?
In the "Application_Error" routine, Server.GetLastError() returns exceptions of type 'System.Web.HttpUnhandledException' because you did not handle the error earlier - i.e. at method or page level.
You need to examine the contents of "InnerException" from the exception returned by Server.GetLastError().
The "InnerException" will contain your "bizException".
Dim myException As bizException = DirectCast(Server.GetLastError().InnerException, bizException)
Not sure of the VB.NET syntax here - more of a C# person myself.
Try this instead:
Dim myException As bizException = DirectCast(Server.GetLastError(), bizException)
(Removed "GetBaseException" call.)
精彩评论