标签:
1 Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error 2 Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString)) 3 End Sub
在Page的Error事件处理函数Page_Error中编写处理逻辑。
1 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 2 Me.ErrorPage = "errorInfo.aspx" 3 End Sub
要让ErrorPage属性生效需要在Web.config中将customErrors 的 mode 设置为On
1 <configuration> 2 <system.web> 3 <customErrors mode="On"/> 4 </system.web> 5 </configuration>
3. 应用程序级捕获未处理异常 - Web.config
1 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> 2 <error statusCode="403" redirect="NoAccess.htm" /> 3 <error statusCode="404" redirect="FileNotFound.htm" /> 4 </customErrors>
通过设置customErrors 节点来捕获未处理异常,mode属性有以下三种预设值,
4. 应用程序级捕获未处理异常 - Global.asax
1 Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 2 ‘ Code that runs when an unhandled error occurs 3 Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString)) 4 End Sub
.NET提供了四种错误处理机制,它们有一定的优先级 顺序:Page_Error事件 > ErrorPage属性 > Application_Error事件 > <customErrors>配置项 。
标签:
原文地址:http://www.cnblogs.com/irresistable/p/4726372.html