您的位置:首页 > 编程语言 > ASP

Asp.net 未处理异常

2015-08-13 15:32 567 查看
页面级捕获未处理异常 - Page 的 Error 事件

Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString))
End Sub


在Page的Error事件处理函数Page_Error中编写处理逻辑。

页面级捕获未处理异常 - Page 的 ErrorPage 属性

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.ErrorPage = "errorInfo.aspx"
End Sub


要让ErrorPage属性生效需要在Web.config中将customErrors 的 mode 设置为On

<configuration>
<system.web>
<customErrors mode="On"/>
</system.web>
</configuration>


3. 应用程序级捕获未处理异常 - Web.config

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>


通过设置customErrors 节点来捕获未处理异常,mode属性有以下三种预设值,

Off - 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息,即黄页。

RemoteOnly - 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。只对不在本地 Web 服务器上运行的用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便不向远程客户端显示应用程序的详细信息。

On - 表示在本地和远程用户都会看到自定义错误信息(友好的)。

  4. 应用程序级捕获未处理异常 - Global.asax

    

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString))
End Sub


.NET提供了四种错误处理机制,它们有一定的优先级 顺序:Page_Error事件 > ErrorPage属性 > Application_Error事件 > <customErrors>配置项 。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: