转:如何捕获winform程序全局异常?
前言
上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常。但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了。本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家参考。
处理多线程程序的全局异常demo
好了下面直接上代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading; namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
ThreadExceptionHandler handler = new ThreadExceptionHandler();
// 设置没有没捕获的异常在这里强制被捕获
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
// 注册UI线程异常事件
Application.ThreadException += handler.Form1_UIThreadException;
// 注册非UI线程异常事件
AppDomain.CurrentDomain.UnhandledException += handler.CurrentDomain_UnhandledException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
} internal class ThreadExceptionHandler
{
/// <summary>
/// 捕获UI线程的异常
/// </summary>
/// <param name="sender"></param>
/// <param name="t"></param>
public void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
DialogResult result = DialogResult.Cancel;
try
{
result = ShowThreadExceptionDialog("Windows Forms UI错误", t.Exception);
}
catch
{
try
{
MessageBox.Show("严重的错误","Windows Forms UI错误", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
} // 点中止时退出程序
if (result == DialogResult.Abort)
Application.Exit();
} /// <summary>
/// 捕获非UI线程的异常,
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
DialogResult result = DialogResult.Cancel;
try
{
Exception ex = (Exception)e.ExceptionObject;
result = ShowThreadExceptionDialog("非UI线程错误", ex);
}
catch (Exception exc)
{
try
{
MessageBox.Show("严重的非UI线程错误:" + exc.Message, "非UI线程错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
Application.Exit();
}
}
// 点中止时退出程序
if (result == DialogResult.Abort)
Application.Exit();
} /// <summary>
/// 创建错误信息并显示
/// </summary>
/// <param name="title"></param>
/// <param name="e"></param>
/// <returns></returns>
private DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
string errorMsg = "应用程序错误,请联系管理员," + "错误信息:\n\n";
errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
// 在这边记下日志,一般情况下我们可以自定义日志 TODO
return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Stop);
}
}
}
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.Reflection;
using System.Threading; namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, System.EventArgs e)
{
throw new IndexOutOfRangeException("无效的异常操作");
} private void button2_Click(object sender, System.EventArgs e)
{
Thread th = new Thread(new ThreadStart(ThreadStart1));
th.Start();
}
private void ThreadStart1()
{
throw new FormatException("多线程异常,格式异常");
}
}
}
来自于:http://vsdot.net/archives/1228.htm
转:如何捕获winform程序全局异常?的更多相关文章
- 如何捕获winform程序全局异常?
1.在C#中我们如何处理异常? 上面的问题学过C#的问题大家可能都能回答处理,用try-catch-finally具体如下: try { //可能出错的语句 } catch (Exception) { ...
- 如何捕获winform程序全局异常?(续)
前言 上篇文章我提供了一种方案可以供我们捕获单线程程序中的所有未处理异常.但是如果程序是多线程,那么新增线程出现了异常上个方案就无能为力了.本着方案总比问题多的态度,我再给大家提供一种新的方案,供大家 ...
- 使用Microsoft.ExceptionMessageBox.dll捕获WinForm程序中异常信息并弹窗显示
WinForm程序开发中,在开发模式下对于异常的处理一般都是通过调试的方式来查找异常发生的未知与原因. 下面以“除数为0”的情况来具体说明. Button按钮事件如下: private void bu ...
- winform 记录全局异常捕获
这篇文章主要是备用 记录winform程序捕获全局异常. /// <summary> /// 应用程序的主入口点. /// </summary> public static A ...
- WinForm程序全局捕捉异常处理办法
如何全局捕捉Winform程序异常呢,当然是从程序启动入口的Program类下的Main()方法定义了,下面看下这个类怎么写的吧 static class Program { static strin ...
- c#winform将全局异常抛出,不用大量写try()catch()
一.在program.cs处完善成如下,但是这样后只能抛出主线程(UI)的错误,所以请看第二步 /// 应用程序的主入口点. /// </summary> [STAThread] stat ...
- winform捕捉全局异常
/// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static void Main() { try { //设置应 ...
- C#中的那些全局异常捕获
1.WPF全局捕获异常 public partial class App : Application { public App() { // 在异 ...
- (转)C#中的那些全局异常捕获
C#中的那些全局异常捕获(原文链接:http://www.cnblogs.com/taomylife/p/4528179.html) 1.WPF全局捕获异常 public partia ...
随机推荐
- Could not autowire. No beans of 'TbItemMapper' type found. less... (Ctrl+F1) Checks autowiring prob
Intellij Idea开发工具在@Autowired或者@Resource注入XxxMapper接口时报如下错误: Could not autowire. No beans of 'TbItemM ...
- windows版本下ELK配置
windows版本的es和kibana相对配置起来很简单,网上已经有很多例子都是正确的,只需按照步骤安装一下即可. 主要logstash,网上说的方案,大都不能配置成功,或者总有一些错误,下面把我自己 ...
- 多线程2.md
# 多线程 VS 多进程 - 程序:一堆代码以文本形式存入一个文档 - 进程: 程序运行的一个状态 - 包含地址空间.内存.数据栈等 - 每个进程由自己完全独立的运行环境,多进程共享数据是一个问题 ...
- [路径规划] VFF和VFH
VFF虚拟力场法 #ifndef VFF_HEADER #define VFF_HEADER #include <vector> #include "utils\point.h& ...
- C语言数据类型运算法则
整形与整形运算得到的还是整形 printf("%d\n",1/3); //0 printf("%d\n",1+2); //3 整形与浮点型运算得到浮点型数据 p ...
- java.lang.UnsatisfiedLinkError: dlopen failed: library "libsqlite.so" not found
项目在7.0以下系统的手机上运行正常,但在7.0的手机上运行异常. 出现这个问题的原因是:从 Android 7.0 开始,Android系统将阻止应用动态链接非公开 NDK 库. 解决方法有两种 第 ...
- (转)Fabric 1.0 读写集
本文译自Fabric 1.0 文档,这篇文档详述了当前读写集语义实现的细节.文档地址为: https://hyperledger-fabric.readthedocs.io/en/latest/rea ...
- keras后端设置【转载】
转自:https://keras.io/backend/ At this time, Keras has three backend implementations available: the Te ...
- jq closet的使用,找到距离最近的一个父元素;
- 16个PHP设计模式详解
说明:该教程全部截选自实验楼教程[16个PHP设计模式详解]:主要介绍16个常用的设计模式的基础概念和技术要点,通过UML类图帮助理解设计模式中各个类之间的关联关系,针对每种设计模式都使用PHP完成了 ...