- Page-level Catch Unhandled Exception-page Error event
1 Protected Sub Page_Error (ByVal asObjectByValasHandlesMe . Error2 Response.Redirect ("errorinfo.aspx? errmsg=unhandled&detail=" & System.Web.HttpUtility.UrlEncode ( Server.GetLastError.ToString))3 End Sub
Write the processing logic in the page's Error event handler Page_Error.
- Page-level Capture Unhandled Exception-page's ErrorPage property
1 Protected Sub Page_Load (ByValasObjectByValasHandlesMe. Load2 Me"errorinfo.aspx" 3 End Sub
To make the ErrorPage property effective requires that mode in customerrors be set to on
in Web. config 1 < configuration > 2 > 3 < customerrors mode = "on" /> 4 </ system.web > 5 </ configuration >
3. Application-level Catch unhandled exception-Web. config
1 <customErrorsMode= "RemoteOnly"defaultredirect= "Genericerrorpage.htm">2 <ErrorStatusCode= "403"redirect= "Noaccess.htm" />3 <ErrorStatusCode= "404"redirect= "Filenotfound.htm" />4 </customErrors>
By setting the customerrors node to catch unhandled exceptions, the mode attribute has the following three preset values,
-
-
- Off-Disables custom error messages, both local and remote users will see detailed error messages, the Yellow pages.
- RemoteOnly-Indicates that the local user will see the detailed error message, and the remote user will see the custom error message. Displays custom (friendly) information only for users who are not running on the local Web server. For security purposes, this setting is recommended so that the details of the application are not displayed to remote clients.
- On-Indicates that a custom error message (friendly) is visible to both local and remote users.
4. Application-level Catch unhandled exception-Global.asax
1 Sub Application_Error (ByValasObjectByVal as EventArgs)2 ' Code that runs if an unhandled error occurs3 Response.Redirect ( " errorinfo.aspx? errmsg=unhandled&detail=" & System.Web.HttpUtility.UrlEncode ( Server.GetLastError.ToString))4 End Sub
. NET provides four error handling mechanisms, which have a certain order of precedence: Page_Error events > ErrorPage properties > Application_Error events > <customErrors> configuration Items.
ASP. NET Unhandled exception