1. using System;
  2. using System.Configuration;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using ZB.QueueSys.Common;
  6.  
  7. namespace ZB.QueueSys
  8. {
  9. static class Program
  10. {
  11. /// <summary>
  12. /// 应用程序的主入口点。
  13. /// </summary>
  14. [STAThread]
  15. static void Main()
  16. {
  17. Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
  18. //处理UI线程异常
  19. Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
  20. //处理非UI线程异常
  21. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  22.  
  23. Application.EnableVisualStyles();
  24. Application.SetCompatibleTextRenderingDefault(false);
  25. //数据库类型
  26. DBHelper.Instance.DBType = int.Parse(ConfigurationManager.AppSettings["DBType"]);
  27. //数据库连接串
  28. DBHelper.Instance.ConnectionStr = ConfigurationManager.ConnectionStrings["DBStr"].ConnectionString;
  29. Application.Run(new MainForm());
  30. //Application.Run(new QueueForm());
  31. }
  32.  
  33. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  34. {
  35. string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
  36. LogHelper.Instance.SaveText(str);
  37. Exception error = e.ExceptionObject as Exception;
  38. MessageBox.Show(error.Message, "系统异常提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
  39. }
  40.  
  41. private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  42. {
  43. string str = GetExceptionMsg(e.Exception, e.ToString());
  44. LogHelper.Instance.SaveText(str);
  45. MessageBox.Show(e.Exception.Message, "系统异常提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
  46. }
  47.  
  48. /// <summary>
  49. /// 生成自定义异常消息
  50. /// </summary>
  51. /// <param name="ex">异常对象</param>
  52. /// <param name="backStr">备用异常消息:当ex为null时有效</param>
  53. /// <returns>异常字符串文本</returns>
  54. private static string GetExceptionMsg(Exception ex, string backStr)
  55. {
  56. StringBuilder sb = new StringBuilder();
  57. sb.AppendLine("****************************异常文本****************************");
  58. sb.AppendLine("【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
  59. if (ex != null)
  60. {
  61. sb.AppendLine("【异常类型】:" + ex.GetType().Name);
  62. sb.AppendLine("【异常信息】:" + ex.Message);
  63. sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
  64. sb.AppendLine("【异常方法】:" + ex.TargetSite);
  65. }
  66. else
  67. {
  68. sb.AppendLine("【未处理异常】:" + backStr);
  69. }
  70. sb.AppendLine("***************************************************************");
  71.  
  72. return sb.ToString();
  73. }
  74.  
  75. }
  76. }

  

C#中全局处理异常方式的更多相关文章

  1. android中全局异常捕捉

    android中全局异常捕捉 只要写代码就会有bug,但是我们要想办法收集到客户的bug.有第三方bugly或者友盟等可以收集.但是,android原生就提供了有关收集异常的api,所以我们来学习一下 ...

  2. Spring Boot 中全局异常处理器

    Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...

  3. C#中异步编程异常的处理方式

    异步编程异常处理 在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.但是对于异步编程来说,异常处理一直 ...

  4. Android中处理崩溃异常

    转自:http://my.eoe.cn/817027/archive/17997.html 大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不 ...

  5. Android中处理崩溃异常和记录日志

    大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...

  6. Android中处理崩溃异常CrashHandler

    来源:http://blog.csdn.net/liuhe688/article/details/6584143 大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程 ...

  7. 【转】Android中处理崩溃异常

    大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...

  8. swoole中退出、异常与错误的处理笔记

    关于PHP这方面的知识 可以看 https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html 进行补课 然后下面记录一下使用swoole的时候需要注意的地方 ...

  9. spring mvc 全局处理异常

    spring框架支持很多种全局处理异常的方式 一.Spring MVC处理异常有4种方式: (1)使用Spring-MVC提供的SimpleMappingExceptionResolver: (2)实 ...

随机推荐

  1. Oracle自我补充之trunc()函数使用介绍

    oracle trunc函数使用介绍 核心提示:oracle trunc函数使用介绍 1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC ...

  2. Linux内存压力测试stressapptest

    /********************************************************************** * Linux内存压力测试stressapptest * ...

  3. Gym - 101002K:YATP (树分治+二分+斜率优化)

    题意:给定带点权边权的树,定义路径的花费=路径边权和e+起点点权w[s]*终点点权w[t].N<2e5,e,w<1e6: 思路:首先,需要树分治. 然后得到方程dp[i]=min{ dis ...

  4. dfs——皇后问题(回溯)

    #include <iostream> using namespace std; ],b[],c[],d[]; ; dfs(int i) { if(i>n) { sum++; ) { ...

  5. 基本数据类型,数字int字符串str

    基本数据类型 数字 int 字符串 str 布尔值 bool 列表 list 字典 dict 元组 tuple(待续...) 整数 int - 创建 a = 123 a = int(123) - 转换 ...

  6. hdu1796 How many integers can you find 容斥原理

    Now you get a number N, and a M-integers set, you should find out how many integers which are small ...

  7. 用flask和长轮询实现对帅哥投票和实时查看票数

    flask中的代码 from flask import Flask,request,render_template,redirect,session,jsonify import uuid impor ...

  8. 100 webhook implementations

    转自: https://streamdata.io/blog/100-webhook-implementations/  很不错的整理 What is the scope of the event-d ...

  9. deno学习三 官方提供的方便deno 安装方式

    早起deno 使用了golang 开发,同时需要protobuf 进行数据的序列化以及反序列化处理 当前的deno 已经使用rust 进行了开发,同时官方提供的安装方式也很方便了,不需要 那么复杂的编 ...

  10. MySQL中如何实现 select top n

    mysql 没有 top n 语法,mysql 用 limit 来实现相关功能,而且功能更加强大. 语法: SELECT * FROM table LIMIT [offset,] rows | row ...