Winform Application.SetUnhandledExceptionMode Method

2023-11-13

Application.SetUnhandledExceptionMode 方法 可以设置程序的一场处理,
参数是一个 UnhandledExceptionMode 的枚举

参数值 说明
Automatic 将所有异常路由到 ThreadException 处理程序,除非应用程序的配置文件另有指定。
CatchException 总是将异常路由抛到 ThreadException 处理程序。忽略应用程序配置文件。
ThrowException 不要将异常路由到 ThreadException 处理程序。忽略应用程序配置文件。

通过设置 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 可以将所有异常强制到我们的处理程序里面处理,包括UI异常,线程异常等等

Thread newThread = null;

// Starts the application.
public static void Main(string[] args)
{
    // Add the event handler for handling UI thread exceptions to the event.
    Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

    // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    // Add the event handler for handling non-UI thread exceptions to the event.
    AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    // Runs the application.
    Application.Run(new ErrorHandlerForm());
}

// Programs the button to throw an exception when clicked.
private void button1_Click(object sender, System.EventArgs e)
{
    throw new ArgumentException("The parameter was invalid");
}

// Start a new thread, separate from Windows Forms, that will throw an exception.
private void button2_Click(object sender, System.EventArgs e)
{
    ThreadStart newThreadStart = new ThreadStart(newThread_Execute);
    newThread = new Thread(newThreadStart);
    newThread.Start();
}

// The thread we start up to demonstrate non-UI exception handling.
void newThread_Execute()
{
    throw new Exception("The method or operation is not implemented.");
}

// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
private static void Form1_UIThreadException(object sender, ThreadExceptionEventArgs t)
{
    DialogResult result = DialogResult.Cancel;
    try
    {
        result = ShowThreadExceptionDialog("Windows Forms Error", t.Exception);
    }
    catch
    {
        try
        {
            MessageBox.Show("Fatal Windows Forms Error",
                "Fatal Windows Forms Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
        }
        finally
        {
            Application.Exit();
        }
    }

    // Exits the program when the user clicks Abort.
    if (result == DialogResult.Abort)
        Application.Exit();
}

// Handle the UI exceptions by showing a dialog box, and asking the user whether
// or not they wish to abort execution.
// NOTE: This exception cannot be kept from terminating the application - it can only
// log the event, and inform the user about it.
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    try
    {
        Exception ex = (Exception)e.ExceptionObject;
        string errorMsg = "An application error occurred. Please contact the adminstrator " +
            "with the following information:\n\n";

        // Since we can't prevent the app from terminating, log this to the event log.
        if (!EventLog.SourceExists("ThreadException"))
        {
            EventLog.CreateEventSource("ThreadException", "Application");
        }

        // Create an EventLog instance and assign its source.
        EventLog myLog = new EventLog();
        myLog.Source = "ThreadException";
        myLog.WriteEntry(errorMsg + ex.Message + "\n\nStack Trace:\n" + ex.StackTrace);
    }
    catch (Exception exc)
    {
        try
        {
            MessageBox.Show("Fatal Non-UI Error",
                "Fatal Non-UI Error. Could not write the error to the event log. Reason: "
                + exc.Message, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }
        finally
        {
            Application.Exit();
        }
    }
}

// Creates the error message and displays it.
private static DialogResult ShowThreadExceptionDialog(string title, Exception e)
{
    string errorMsg = "An application error occurred. Please contact the adminstrator " +
        "with the following information:\n\n";
    errorMsg = errorMsg + e.Message + "\n\nStack Trace:\n" + e.StackTrace;
    return MessageBox.Show(errorMsg, title, MessageBoxButtons.AbortRetryIgnore,
        MessageBoxIcon.Stop);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Winform Application.SetUnhandledExceptionMode Method 的相关文章

  • OpenGL缓冲区更新[重复]

    这个问题在这里已经有答案了 目前我正在编写一个模拟水的程序 以下是我所做的步骤 创建水面 平面 创建VAO 创建顶点缓冲区对象 在其中存储法线和顶点 将指针绑定到此 VBO 创建索引缓冲区对象 然后我使用 glDrawElements 渲染
  • 在 Mac OS X 上安装 libxml2 时出现问题

    我正在尝试在我的 Mac 操作系统 10 6 4 上安装 libxml2 我实际上正在尝试在 Python 中运行 Scrapy 脚本 这需要我安装 Twisted Zope 现在还需要安装 libxml2 我已经下载了最新版本 2 7 7
  • 从时间列表中查找最接近的时间

    所以 这是场景 我有一个带有创建时间的文件 我想从该文件的创建时间最接近或相等的时间列表中选择一个时间 完成此操作的最佳方法是什么 var closestTime listOfTimes OrderBy t gt Math Abs t fi
  • 为什么 std::function 不是有效的模板参数,而函数指针却是?

    我已经定义了名为的类模板CallBackAtInit其唯一目的是在初始化时调用函数 构造函数 该函数在模板参数中指定 问题是模板不接受std function作为参数 但它们接受函数指针 为什么 这是我的代码 include
  • 如何设置消息队列的所有者?

    System Messaging MessageQueue 类不提供设置队列所有权的方法 如何以编程方式设置 MSMQ 消息队列的所有者 简短的答案是 p invoke 对 windows api 函数的调用MQSetQueueSecuri
  • 将 2 个字节转换为整数

    我收到一个 2 个字节的端口号 最低有效字节在前 我想将其转换为整数 以便我可以使用它 我做了这个 char buf 2 Where the received bytes are char port 2 port 0 buf 1 port
  • 如何在 EF Core 2.1 中定义外键关系

    我的 DAL 使用 EF Core 2 1 这就是我的模型的样子 一名用户只能拥有一种角色 Role entity kind of master public class Role public int RoleId get set pub
  • MSChart 控件中的自定义 X/Y 网格线

    我有一个带有简单 2D 折线图的 C Windows 窗体 我想向其中添加自定义 X 或 Y 轴标记 并绘制自定义网格线 例如 以突出显示的颜色 虚线 我查看了 customLabels 属性 但这似乎覆盖了我仍然想显示的默认网格 这是为了
  • C 与 C++ 中的 JNI 调用不同?

    所以我有以下使用 Java 本机接口的 C 代码 但是我想将其转换为 C 但不知道如何转换 include
  • 在 C++ 代码 gdb 中回溯指针

    我在运行 C 应用程序时遇到段错误 在 gdb 中 它显示我的一个指针位置已损坏 但我在应用程序期间创建了 10 万个这样的对象指针 我怎样才能看到导致崩溃的一个 我可以在 bt 命令中执行任何操作来查看该指针的生命周期吗 谢谢 鲁奇 据我
  • 使用 Unity 在 C# 中发送 http 请求

    如何使用 Unity 在 C 中发送 HTTP GET 和 POST 请求 我想要的是 在post请求中发送json数据 我使用Unity序列化器 所以不需要 新的 我只想在发布数据中传递一个字符串并且能够 将 ContentType 设置
  • 在 Qt 中播放通知(频率 x)声音 - 最简单的方法?

    Qt 5 1 或更高版本 我需要播放频率为 x 的通知声音 n 毫秒 如果我能像这样组合音调那就太好了 1000Hz 持续 2 秒 然后 3000Hz 持续 1 秒 最简单的方法是使用文件 WAV MP3 例如如此处所述 如何用Qt播放声音
  • 不使用放置 new 返回的指针时的 C++ 严格别名

    这可能会导致未定义的行为吗 uint8 t storage 4 We assume storage is properly aligned here int32 t intPtr new void storage int32 t 4 I k
  • 在二进制数据文件的标头中放入什么

    我有一个模拟 可以读取我们创建的大型二进制数据文件 10 到 100 GB 出于速度原因 我们使用二进制 这些文件依赖于系统 是从我们运行的每个系统上的文本文件转换而来的 所以我不关心可移植性 当前的文件是 POD 结构的许多实例 使用 f
  • 解释这段代码的工作原理;子进程如何返回值以及在哪里返回值?

    我不明白子进程如何返回该值以及返回给谁 输出为 6 7 问题来源 http www cs utexas edu mwalfish classes s11 cs372h hw sol1 html http www cs utexas edu
  • 在 C 中使用 #define 没有任何价值

    If a define没有任何价值地使用 例如 define COMMAND SPI 默认值是0吗 不 它的评估结果为零 从字面上看 该符号被替换为空 然而 一旦你有了 define FOO 预处理器条件 ifdef FOO现在将是真的 另
  • MSVC编译器下使用最大成员初始化联合

    我正在尝试初始化一个LARGE INTEGER在 C 库中为 0 确切地说是 C 03 以前 初始化是 static LARGE INTEGER freq 0 在 MinGW 下它产生了一个警告 缺少成员 LARGE INTEGER Hig
  • 如何使用 VB.NET 打开受密码保护的共享网络文件夹?

    我需要在网络上打开受密码保护的共享文件夹才能访问 Access 97 数据库 如何打开文件夹并输入密码 在这里找到http www mredkj com vbnet vbnetmapdrive html http www mredkj co
  • 如何知道 HTTP 请求标头值是否存在

    我确信这很简单 但是却让我感到厌烦 我在 Web 应用程序中使用了一个组件 它在 Web 请求期间通过添加标头 XYZComponent true 来标识自身 我遇到的问题是 如何在视图中检查此组件 以下内容不起作用 if Request
  • 是否可以使用 Dapper 流式传输大型 SQL Server 数据库结果集?

    我需要从数据库返回大约 500K 行 请不要问为什么 然后 我需要将这些结果保存为 XML 更紧急 并将该文件通过 ftp 传输到某个神奇的地方 我还需要转换结果集中的每一行 现在 这就是我正在做的事情 TOP 100结果 使用 Dappe

随机推荐

  • 【泛微E9开发】workflowservice创建流程

    最下面附demo下载地址 包括所需要的JAR文件 package test WorkflowServicePortType import org junit Test import weaver workflow webservices W
  • python创意作品-python的作品

    广告关闭 2017年12月 云 社区对外发布 从最开始的技术博客到现在拥有多个社区产品 未来 我们一起乘风破浪 创造无限可能 发现了编程与艺术又一个契合点 小开心一下 其实这个过程非常简单 我们先看作品 后讲解代码 python书法作品1
  • 自定义TableLayoutPanel使它能够在运行时用鼠标改变行高和列宽。

    using System using System Collections Generic using System ComponentModel using System Drawing using System Windows Form
  • 1054 求平均值 (20 分)

    1054 求平均值 20 20 分 本题的基本要求非常简单 给定N个实数 计算它们的平均值 但复杂的是有些输入数据可能是非法的 一个 合法 的输入是 1000 1000 区间内的实数 并且最多精确到小数点后2位 当你计算平均值的时候 不能把
  • TiKV架构解析

    TiKV架构解析 TiKV 的整体架构比较简单 如下 参考资料 1 TiKV 源码解析系列 如何使用 Raft 2 TiKV 源码解析系列 multi raf
  • Connection timed out: connect. If you are behind an HTTP proxy, please configure the proxy settings

    Connection timed out connect If you are behind an HTTP proxy please configure the proxy settings either in IDE or Gradle
  • 我的2018年总结

    前言 本来没有打算总结自己的2018年 毕竟自己就是个普通的不能再普通的学生 没有什么特别值得让人关注的地方 但是今天看到了自己的好朋友昨天写了他的2018年总结 看了感觉记录一下自己的生活还是挺有意义的 所以就也打算稍微写一点 毕竟写这些
  • 启动Nginx报[10013]错误的解决方案

    报错情景 今天自己再本地配置好Nginx 但是启动时报了 10013 的错误 上网查了下 原因是80端口被占用了 错误提示如下图 随后在cmd中输入下列命令 如图示 查看了一下80端口的占用情况 发现果然被占用 情况和网上其他人所遇到的是一
  • Nacos使用域名做为服务地址遇到的问题及解决方案

    一 发现问题 应用启动时 增加Nacos服务端的配置信息 应用使用IP加端口连接Nacos服务器时 运行一切正常 启动参数增加以下Nacos参数 Dspring cloud nacos discovery namespace DEV Dsp
  • NUC980开源项目28-error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was

    上面是我的微信和QQ群 欢迎新朋友的加入 项目码云地址 国内下载速度快 https gitee com jun626 nuc980 open source project 项目github地址 https github com Jun117
  • ES6 -- Iterator 的基本用法

    1 Iterator作用 1 为各种数据 提供一个统一的 简便的访问接口 2 使数据结构的成员能够按某种次序排列 3 ES6创造了一种新的遍历命令for of循环 Iterator接口主要供for of消费 2 Iterator 的遍历过程
  • 华为ensp---组播服务器实验

    一 实验拓扑 ensp里选择MCS为组播服务器 二 设置VLC参数 点击ensp右上角的设置 在工具设置里面把VLC的安装路径选上 三 详细配置 1 组播服务器配置 2 PC端配置 输入MCS组播的IP和MAC地址 2 路由器配置
  • NPM和webpack的关系(转载)

    入门前端的坑也很久了 以前很多大小项目 前端都是传统式开发 一直在重复造轮子 接触VUE后 对vue cli有了解后 仅仅知道vue cli是一个vue项目的脚手架 可以快速的构建一个vue的基于npm的模块化项目 vue内部的打包机制其实
  • IDEA中SonarLint插件的安装与配置

    本文内容概要 本文介绍了IDEA SonarLint插件的装 以及配置SonarLint使用 SonarQube的规则 注意 不含有SonarQube安装和使用 代码管理Sonar和SonarLint简介 Sonar简介摘自sonar百度百
  • Pycharm设置注释行字体和颜色的方法

    第一步 进入 file gt settings gt Editor gt Color Scheme gt python 选择 Line Commet 然后点击 Foreground 选择颜色 大家可以设置自己夏欢注释字体的颜色 我举例设置成
  • zabbix监控多实例redis

    Zabbix监控多实例Redis 软件名称 软件版本 Zabbix Server 6 0 17 Zabbix Agent 5 4 1 Redis 6 2 10 Zabbix客户端配置 编辑自动发现脚本 vim usr local zabbi
  • 三. Consul 作为 SpringCloud 注册中心配置

    目录 一 Consul 简单介绍 1 Consul 的 windows 单机版安装运行 二 配置服务注册到 Consul 1 服务提供方 yml 文件配置注册到 Consul 2 服务消费方 yml 文件配置注册到 Consul 3 服务消
  • vue使用video.js实现播放m3u8格式的视频

    一 安装video js npm install video js 我使用的video js版本如下 video js 7 20 3 二 VideoPlayer组件 在components下新建一个VideoPlayer文件夹 index如
  • Allegro各层简介

    1 Allegro的整体分层可以分为大类Class和小类Subclass 常用的Class如下 Board Geometry 总体层叠 做板框 标注和丝印等等 Etch 铜皮 电气连线 Boundary Package Geometry 封
  • Winform Application.SetUnhandledExceptionMode Method

    Application SetUnhandledExceptionMode 方法 可以设置程序的一场处理 参数是一个 UnhandledExceptionMode 的枚举 参数值 说明 Automatic 将所有异常路由到 ThreadEx