错误异常处理方法
2012-02-21 23:46
302 查看
我们都知道异常可以用 try-catch-finally来捕获并处理异常,这是在程序运行时可以捕获处理的。
下面介绍几种开发上常用的错误处理
包括:
aps.net 异常处理
1、页面级错误处理
2、应用程序级错误处理
3、应用程序配置
WinForm应用程序全局异常处理
一、asp.net页面级错误处理
在单独页面中的错误。可以在page_error事件中添加处理逻辑代码,通过Server.GetLastError()获取当前页面的错误。
如果少掉了server.clearError()方法的话,仍然会引发Application_Error的错误处理。
二、应用程序级错误处理
截获整个应用程序中运行的错误,可以在项目的Global.asax文件中的 application_error 添加处理逻辑。
三、应用程序配置
可以在web.config中配置一些常见错误的处理方法
其中各元素的用途如下。
1、mode:具有On , Off ,RemoteOnly 三种状态。
on:表示始终显示自定义的信息。
off:表示始终显示详细的asp.net错误信息。
RemoteOnly:表示只对不在本地web服务器上运行的用户显示自定义信息。
2、defaultRedirect:用于出现错误时重定向的URL地址(可选)。
3、statusCode : 指明错误状态码,表明一种特定的出错状态。
4、redirect : 错误的重定向URL
四、winform应用程序全局异常处理
winform中没有像asp.net的 application_error事件,但我们可以通过声明委托的方式来实现全局的异常截获。
Program.cs代码
下面介绍几种开发上常用的错误处理
包括:
aps.net 异常处理
1、页面级错误处理
2、应用程序级错误处理
3、应用程序配置
WinForm应用程序全局异常处理
一、asp.net页面级错误处理
在单独页面中的错误。可以在page_error事件中添加处理逻辑代码,通过Server.GetLastError()获取当前页面的错误。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _22 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Page_Error(object sender, EventArgs e) { string errMsg = ""; Exception currentError = Server.GetLastError(); errMsg += "系统发生错误:<br/>" + "错误地址: " + Request.Url.ToString() + "<br/>" + "错误信息: " + currentError.Message.ToString() + "<br/>"; Response.Write(errMsg); Server.ClearError();//要注意这句代码的使用,清除异常。 } } }
如果少掉了server.clearError()方法的话,仍然会引发Application_Error的错误处理。
二、应用程序级错误处理
截获整个应用程序中运行的错误,可以在项目的Global.asax文件中的 application_error 添加处理逻辑。
protected void Application_Error(object sender, EventArgs e) { Exception ex = Server.GetLastError(); string errmsg = ""; string Particular = ""; if (ex.InnerException != null) { errmsg = ex.InnerException.Message; Particular = ex.InnerException.StackTrace; } else { errmsg = ex.Message; Particular = ex.StackTrace; } //AddLog(errmsg, Particular); Server.ClearError();//处理完及时清理异常 }
三、应用程序配置
可以在web.config中配置一些常见错误的处理方法
<system.web> <customErrors mode="On" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm"/> <error statusCode="404" redirect="FileNotFound.htm"/> </customErrors> </system.web>
其中各元素的用途如下。
1、mode:具有On , Off ,RemoteOnly 三种状态。
on:表示始终显示自定义的信息。
off:表示始终显示详细的asp.net错误信息。
RemoteOnly:表示只对不在本地web服务器上运行的用户显示自定义信息。
2、defaultRedirect:用于出现错误时重定向的URL地址(可选)。
3、statusCode : 指明错误状态码,表明一种特定的出错状态。
4、redirect : 错误的重定向URL
四、winform应用程序全局异常处理
winform中没有像asp.net的 application_error事件,但我们可以通过声明委托的方式来实现全局的异常截获。
Program.cs代码
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Threading; namespace _23 { static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { // 定义线程异常事件 ThreadExceptionHandler handler = new ThreadExceptionHandler(); Application.ThreadException += new ThreadExceptionEventHandler(handler.Application_ThreadException); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace _23 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { throw new InvalidOperationException("无效的操作异常"); } } // 异常处理类. internal class ThreadExceptionHandler { // 实现错误异常事件. public void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { try { // 如果用户点击“Abort”按钮则退出应用程序。 DialogResult result = ShowThreadExceptionDialog(e.Exception); if (result == DialogResult.Abort) Application.Exit(); } catch { try { MessageBox.Show("严重错误", "严重错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); } finally { Application.Exit(); } } } // 输出错误信息 private DialogResult ShowThreadExceptionDialog(Exception ex) { string errorMessage = "错误信息:\n\n" + ex.Message + "\n\n" + ex.GetType() + "\n\nStack Trace:\n" + ex.StackTrace; return MessageBox.Show(errorMessage, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop); } } }
相关文章推荐
- 三种错误处理的方法——从返回值到异常处理
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- 基于PHP7错误处理与异常处理方法(详解)
- PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
- PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
- ASP.NET生成eurl.axd Http异常错误的处理方法
- ASP.NET生成eurl.axd Http异常错误处理方法【转】
- ASP.NET 4.0 生成 eurl.axd Http异常错误的处理方法 (汗IIS 怎这么多莫名其妙的问题)
- java中的常见异常及错误和处理方法
- 项目开发时,出现异常,但控制台没有打印错误信息处理方法
- PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
- ASP.NET生成eurl.axd Http异常错误的处理方法
- 关于IE7打开任何网页弹出[未处理的异常('对象不支持此属性或方法')发生在iexplorer.exe]错误调试框的解决办法
- PyQt 如果异常未处理,抛入到事件循环,则无错误提示崩溃的解决方法
- 代码阅读总结之Fitch and Mather 7.0(asp.net发生异常或错误时错误提示页面的处理方法)
- 从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
- 扩展jQuery的方法统一处理AJAX调用过程中产生的异常错误信息
- 总结Selenium自动化测试方法(六)常见的异常错误处理
- myeclipse异常处理,错误显示顺序的调整方法 推荐
- [转贴]从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介