当前位置: 首页 > news >正文

C#捕捉全局异常

1.运行图片

在这里插入图片描述

2.源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;namespace 捕捉全局异常
{internal static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){//设置捕捉全局异常的程序Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);Application.ThreadException += Application_ThreadException;AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e){//处理应用程序域中的异常Exception ex = e.ExceptionObject as Exception;if (ex != null){MessageBox.Show($"发生了未处理的异常:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}else{MessageBox.Show($"发生了未处理的非托管异常", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e){//处理线程异常MessageBox.Show($"发生了未处理的异常:{e.Exception.Message}\r\n" + $"错误的位置:{e.Exception.StackTrace}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}
}

3,使用方法---->不用弹窗的方式,可以用写Log的方式写出来。

http://www.lryc.cn/news/183808.html

相关文章:

  • java.text.ParseException: Unparseable date: “2023-09-06T09:08:18“
  • macOS 下如何优雅的使用 Burp Suite 汉化
  • 进程同步与进程互斥
  • 公司安防工程简要介绍及系统需求分析
  • JMETER自适应高分辨率的显示器
  • Linux工具(三)
  • 基于SSM+Vue的鲜花销售系统设计与实现
  • 矢量图形编辑软件illustrator 2023 mac特点介绍
  • 【计算机网络面试题(62道)】
  • JVM-满老师
  • 加锁常见的问题
  • 【LeetCode力扣】LCR170 使用归并排序的思想解决逆序对问题(详细图解)
  • python经典百题之一个素数能被几个9整除
  • Thymeleaf 内联语法使用教程
  • Django学习笔记-实现聊天系统
  • C++转换函数
  • Spring Boot中的@Controller使用教程
  • 【17】c++设计模式——>原型模式
  • 金三银四好像消失了,IT行业何时复苏!
  • PDF文件超出上传大小?三分钟学会PDF压缩
  • java入坑之国际化编程
  • Kafka客户端核心参数详解
  • 踩大坑ssh免密登录详细讲解
  • 操作系统八股
  • Hudi SQL DDL
  • gin 框架的 JSON Render
  • 《Dataset Condensation with Differentiable Siamese Augmentation》
  • 多普勒频率相关内容介绍
  • win10睡眠快捷方式
  • C++中的static和extern关键字