一.在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# 捕获全局异常的更多相关文章

  1. 在C#代码中应用Log4Net(四)在Winform和Web中捕获全局异常

    毕竟人不是神,谁写的程序都会有bug,有了bug不可怕,可怕的是出错了,你却不知道错误在哪里.所以我们需要将应用程序中抛出的所有异常都记录起来,不然出了错,找问题就能要了你的命.下面我们主要讨论的是如 ...

  2. Android捕获全局异常

    Android捕获全局异常 程序避免不了出现bug,导致程序崩溃,为了尽量不影响用户体验,可以全局捕获异常 效果图 异常捕获处理前 异常捕获处理后(将程序重新启动) 捕获异常的工具类 package ...

  3. SpringBoot捕获全局异常

    1.创建GloableExceptionAop类捕获全局异常 package com.cppdy.exception; import org.springframework.web.bind.anno ...

  4. Android应用捕获全局异常自定义处理

    [2016-06-30]最新的全局异常处理DRCrashHandler已经集成在DR_support_lib库中 具体请看: https://coding.net/u/wrcold520/p/DR_s ...

  5. WebForm 在 Global.asax 中捕获全局异常

    /// <summary> /// 捕获全局异常 /// </summary> /// <param name="sender">sender& ...

  6. SpringBoot学习笔记(二):SpringBoot访问静态文件、捕获全局异常、集成Thymeleaf、集成JSP

    SpringBoot访问静态文件 什么是静态文件? 不需要通过web容器去得到的文件,直接通过路径就能得到的文件,比如项目的css,js,img等文件. 所有的资源文件都应该在src/main/res ...

  7. C# WinForm捕获全局异常

    网上找的C# WinForm全局异常捕获方法,代码如下: static class Program { /// <summary> /// 应用程序的主入口点. /// </summ ...

  8. express捕获全局异常的三种方法

    场景 express的路由里抛出异常后,全局中间件没办法捕获,需要在所有的路由函数里写try catch,这坑爹的逻辑让人每次都要多写n行代码 官方错误捕获中件间代码如下 app.use(functi ...

  9. 在Global.asax中 注册Application_Error事件 捕获全局异常

    参考于:https://shiyousan.com/post/635813858052755170 在ASP.NET MVC中,通过应用程序生命周期中的Application_Error事件可以捕获到 ...

随机推荐

  1. document.getElementsByTagName()方法的返回值

    在阅读<JS DOM 编程一书>一书时,看到getElementByTagName函数返回值为数组,然后自己验证了下,发现不是数组,而是一个可遍历的HTMLCollection对象 HTM ...

  2. 利用localStorage实现浏览器中多个标签页之间的通信

    原理: localStorage是浏览器存储数据的容器,而且它是多页面共享的,利用localStorage多页面共享的特性,可以实现多个标签页的通信. 比如: 一个标签页发送消息(将发送的消息设置到l ...

  3. R语言 绘图——条形图可以将堆积条形图与百分比堆积条形图配合使用

    在使用堆积条形图时候,新增一个百分比堆积条形图,可以加深读者印象. 封装一个function函数后只需要在调用的数据上改一下pos=‘fill’的代码即可.比较方便. 案例: # 封装函数 fun1& ...

  4. 认识一下Qt用到的开发工具

    http://c.biancheng.net/view/3868.html Qt 不是凭空产生的,它是基于现有工具链打造而成的,它所使用的编译器.链接器.调试器等都不是自己的,Qt 官方只是开发了上层 ...

  5. poj 3714 Raid(平面最近点对)

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7473   Accepted: 2221 Description ...

  6. 针对360浏览器读取不了cookie的问题

    今天学习cookie的时候发现在360和谷歌浏览器下设置cookie打开是空白的!经过一番搜索才知道在本地是访问不了cookie只能在服务器端进行访问,但是仍然可以在火狐下进行访问

  7. npm cache clean --force

    当出现这个问题时npm ERR! Unexpected end of JSON input while parsing near '...,"dist":{"shasum ...

  8. jenkins 邮箱通知设置

    https://blog.csdn.net/boonya/article/details/77335074 https://blog.csdn.net/lovedingd/article/detail ...

  9. echart-折线图,数据太多想变成鼠标拖动和滚动的效果?以及数据的默认圈圈如何自定义圆圈的样式

    1.数据太多怎么办???想拖拽,想滑动 dataZoom: [ { type: 'slider', } ] dataZoom: [ { type: 'inside',  }] 两种功能都需要,还想调样 ...

  10. 前端node面试题之---对比JS和NodeJS的区别

    区别: 1.JS运行在浏览器端,用于用户的交互效果,NodeJS运行在服务器端,用于服务器的操作,例如,Web服务器创建,数据库的操作,文件的操作等 2.JS运行在浏览器端,存在多个JS解释器,存在兼 ...