WinForm 捕获未处理的异常,可以使用Application.ThreadException 和AppDomain.CurrentDomain.UnhandledException事件

WinForm程序的全局错误处理可以在Program文件中设置处理事件,如下:

  1. static class Program
  2. {
  3. /// <summary>
  4. /// 应用程序的主入口点。
  5. /// </summary>
  6. [STAThread]
  7. static void Main()
  8. {
  9. Application.ThreadException += Application_ThreadException;
  10. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  11. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  12. Application.EnableVisualStyles();
  13. Application.SetCompatibleTextRenderingDefault(false);
  14. Application.Run(new MainForm());
  15. }
  16.  
  17. /// <summary>
  18. /// 处理应用程序域内的未处理异常(非UI线程异常)
  19. /// </summary>
  20. /// <param name="sender"></param>
  21. /// <param name="e"></param>
  22. static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  23. {
  24. try
  25. {
  26. Exception ex = e.ExceptionObject as Exception;
  27. MessageBox.Show(ex.Exception.Message);
  28. }
  29. catch { }
  30. }
  31.  
  32. /// <summary>
  33. /// 处理应用程序的未处理异常(UI线程异常)
  34. /// </summary>
  35. /// <param name="sender"></param>
  36. /// <param name="e"></param>
  37. static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  38. {
  39. try
  40. {
  41. MessageBox.Show(e.Exception.Message);
  42. }
  43. catch { }
  44. }
  45. }

这里有一个需要注意的地方,如下代码 Application.Run(new MainForm()); 这里只能执行一次 Application.Run代码,如果你像下面这样写代码的话:

  1. Application.Run(new Form1());
  2. Application.Run(new Form2());

那么如果在Form2出现未处理的异常(UI异常或是非UI异常),上面的异常处理程序就无法捕获了。

出处:https://www.cnblogs.com/bruceleeliya/archive/2013/01/12/2857681.html

WinForm 捕获异常 Application.ThreadException + AppDomain.CurrentDomain.UnhandledException的更多相关文章

  1. Winform异常处理之ThreadException、unhandledException及多线程异常处理

    异常处理之ThreadException.unhandledException及多线程异常处理 一:ThreadException和unhandledException的区别 处理未捕获的异常是每个应 ...

  2. C#中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别

    // 获取程序的基目录. System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径. System.Diagnostics.Process.G ...

  3. AppDomain.CurrentDomain.BaseDirectory是什么

    AppDomain.CurrentDomain.BaseDirectory 是获取基目录,它由程序集冲突解决程序用来探测程序集.由显示的路径可以看出,它代表的是程序集所在的目录,它具有读取和写入的属性 ...

  4. AppDomain.CurrentDomain.BaseDirectory

    在winform中的OnPaint事件中,AppDomain.CurrentDomain.BaseDirectory得到的是下面这个路径 C:\Program Files (x86)\Microsof ...

  5. C#中AppDomain.CurrentDomain.BaseDirectory及各种路径获取方法

    // 获取程序的基目录.System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径,包含文件名System.Diagnostics.Proces ...

  6. .Net AppDomain.CurrentDomain.AppendPrivatePath(@"Libs");

    今天就说说.Net中通过反射取得某个类型时,我们怎么知道这个类型在硬盘上的哪个角落?比如说,假如我们需要要求服务端动态载入某个数据源,那服务端怎么知道数据源在哪?网上大部分的教程都写着,可以使用Ass ...

  7. 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory

    关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...

  8. AppDomain.CurrentDomain.GetAssemblies()

    AppDomain.CurrentDomain.GetAssemblies() ,获取已加载到此应用程序域的执行上下文中的程序集 解释地址 从微软的解释也可以得知,这个方法只能获取已经加载到此应用程序 ...

  9. AppDomain.CurrentDomain.AssemblyResolve

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); 参 ...

随机推荐

  1. Node.js实现PC端类微信聊天软件(一)

    Github StackChat 技术栈 写这个软件StackChat的主要目的是巩固练习Node和对React的实践,也是为了学习东西,所以选用了这些自己还没在项目里使用过的技术,边学变写 Elec ...

  2. 关闭正在执行的事务 Kill

    .模拟资源锁定 --开始事务BEGIN TRANSACTION--更新数据update Table_1 set FuncName=FuncName--等待1分钟WAITFOR DELAY '01:00 ...

  3. java 查看类是从哪个jar包加载的

    package com.jason object FIndjar { def main(args: Array[String]): Unit = { val pd = classOf[org.apac ...

  4. phpstorm打开报错方法

    phpstorm打开报错方法 直接删掉默认文件夹里的idea文件夹 重新打开就可以了

  5. redis常用命令及持久化机制

    redis  常用命令 查找redis服务文件 find / -name  redis-server 查找配置文件 find / -name redis.conf 启动服务时候,要指定配置文件 启动r ...

  6. spring data jpa碰到的坑

    1.不能从别的类的repository那里 执行另一个类的sql,这样映射会失败. 2.有entity,就要有repository,并且还要有id注解 3.还要多表联查未测试,估计要用map去映射出来 ...

  7. c++ pipe实现父子进程通信

    1.父子进程通信pipe编程流程 -创建管道 -设置进程的输出到管道 -创建进程 -关闭管道写句柄 -读管道读句柄,把数据读到一个buffer里 2.注意事项 -读管道数据的时候,一定要关闭写句柄: ...

  8. 最少硬币数——Java

    问题:有n种硬币,面值分别为v1,v2,v3,…,vn,存于数组T[1:n]中,可以使用的各种面值的硬币个数存于数组Coins[1:n]中.对任意钱数0≤m≤20001,设计一个用最少硬币找钱m的方法 ...

  9. GitLab+Jenkins持续集成

    一.概述 GitLab是一个代码仓库,用来管理代码.Jenkins是一个自动化服务器,可以运行各种自动化构建.测试或部署任务.所以这两者结合起来,就可以实现开发者提交代码到GitLab,Jenkins ...

  10. 在linux系统下安装两个nginx以及启动、停止、重起

    如果没有安装过nginx请看:linux下nginx部署以及配置详解 1.第一个nginx已经安装完成后,现在安装第二个nginx 启动:sudo /usr/sbin/nginx3 重起:sudo / ...