您的位置:首页 > 其它

错误异常处理方法

2012-02-21 23:46 302 查看
我们都知道异常可以用 try-catch-finally来捕获并处理异常,这是在程序运行时可以捕获处理的。

下面介绍几种开发上常用的错误处理

包括:

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);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐