C# 捕获全局异常
一.在Winform程序中捕获全局异常
在winfrom中我们需要了解Application对象中的两个事件
①Application.ThreadException 事件--当UI线程中某个异常未被捕获时出现。
②AppDomain.UnhandledException 事件--当非UI线程中某个异常未被捕获时出现。
我们需要在Program.cs中设置异常的捕捉代码(如下图所示)。LogHelper类是自定义的日志帮助类,在前面的几篇文章中已经有涉及到。
需要在Program.cs中添加的代码如下所示


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using Common; namespace testLog4N
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
BindExceptionHandler();//绑定程序中的异常处理
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
/// <summary>
/// 绑定程序中的异常处理
/// </summary>
private static void BindExceptionHandler()
{
//设置应用程序处理异常方式:ThreadException处理
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理未捕获的异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}
/// <summary>
/// 处理UI线程异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
LogHelper.ErrorLog(null, e.Exception as Exception);
}
/// <summary>
/// 处理未捕获的异常
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
LogHelper.ErrorLog(null, e.ExceptionObject as Exception);
}
}
}


二、在Web中捕获全局异常
我们只需要在Global.asax文件中添加异常捕获的代码即可。
完整Global.asax代码如下所示


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Common; namespace WebApplication_testLog4Net
{
public class Global : System.Web.HttpApplication
{ void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码 } void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码 } void Application_Error(object sender, EventArgs e)
{
// 在出现未处理的错误时运行的代码
Exception objExp = HttpContext.Current.Server.GetLastError();
LogHelper.ErrorLog("<br/><strong>客户机IP</strong>:" + Request.UserHostAddress + "<br /><strong>错误地址</strong>:" + Request.Url , objExp);
} void Session_Start(object sender, EventArgs e)
{
// 在新会话启动时运行的代码
} void Session_End(object sender, EventArgs e)
{
// 在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
// 或 SQLServer,则不会引发该事件。 } }
}


三、在WPF中捕获全局异常
我们只需要在App.xaml文件中添加异常捕获的代码即可。
在WPF中捕获全局异常主要涉及到以下两个事件
1、AppDomain.UnhandledException 事件--当某个异常未被捕获时出现。主要指的是非UI线程。
2、Application.DispatcherUnhandledException 事件--如果异常是由应用程序引发,但未处理,发生。主要指的是UI线程。
完整的App.xaml文件如下所示


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Common; namespace WpfApplication1
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
} void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is System.Exception)
{
LogHelper.ErrorLog(null, (System.Exception)e.ExceptionObject);
}
} public static void HandleException(Exception ex)
{
LogHelper.ErrorLog(null,ex);
} void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
e.Handled = true;
LogHelper.ErrorLog(null, e.Exception);
} }
}


C# 捕获全局异常的更多相关文章
- 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常
毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...
- Android捕获全局异常
Android捕获全局异常 程序避免不了出现bug,导致程序崩溃,为了尽量不影响用户体验,可以全局捕获异常 效果图 异常捕获处理前 异常捕获处理后(将程序重新启动) 捕获异常的工具类 package ...
- SpringBoot捕获全局异常
1.创建GloableExceptionAop类捕获全局异常 package com.cppdy.exception; import org.springframework.web.bind.anno ...
- Android应用捕获全局异常自定义处理
[2016-06-30]最新的全局异常处理DRCrashHandler已经集成在DR_support_lib库中 具体请看: https://coding.net/u/wrcold520/p/DR_s ...
- WebForm 在 Global.asax 中捕获全局异常
/// <summary> /// 捕获全局异常 /// </summary> /// <param name="sender">sender& ...
- SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP
SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...
- C# WinForm捕获全局异常
网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...
- express捕获全局异常的三种方法
场景 express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码 官方错误捕获中件间代码如下 app.use(functi ...
- 在Global.asax中 注册Application_Error事件 捕获全局异常
参考于:https://shiyousan.com/post/635813858052755170 在ASP.NET MVC中,通过应用程序生命周期中的Application_Error事件可以捕获到 ...
随机推荐
- CSS链接使用伪类的顺序
顺序为:link-visited-hover-active a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FF00} /* 已访问 ...
- C#批量将数据插入SQLServer数据库
Database db = CreateDatabase(); var varConnnection = db.CreateConnection(); //获取连 ...
- 【玩转Eclipse】——eclipse实现代码块折叠-类似于VS中的#region……#endregion
[玩转Eclipse]——eclipse实现代码块折叠-类似于VS中的#region……#endregion http://www.cnblogs.com/Micheal-G/articles/507 ...
- 远程连接mysql出现1130的错误
数据库权限不足 连接数据以后执行以下命令 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '您的数据库密码' WITH GRANT OP ...
- mknod - 建立块专用或字符专用文件
总览 mknod [options] name {bc} major minor mknod [options] name p GNU 选项(缩写): [-m mode] [--help] [--ve ...
- MongoDB入门_学习目标
MongoDB的概念 MongoDB mongo 索引 集合 复制集 分片 数据均衡 MongoDB数据库搭建 搭建简单的单机服务 搭建具有冗余容错功能的复制集 搭建大规模数据集群 集群的自动部署 熟 ...
- Mongo--02 命令介绍
目录 Mongo工具 1. 查看指令 2.插入命令 3.查询命令 4.更新数据 5.索引 5.删除 6.mongo命令介绍 7.创建用户和角色 Mongo工具 1. 查看指令 test:登录时默认存在 ...
- 001-supervisor
supervisor 使用教程(转) 原文地址:https://word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%9 ...
- java 类记载器
转载: https://blog.csdn.net/javazejian/article/details/73413292
- mysql 分页查询及优化
1.分页查询 select * from table limit startNum,pageSize 或者 select * from table limit pageSize offset star ...