C#中全局处理异常方式
- using System;
- using System.Configuration;
- using System.Text;
- using System.Windows.Forms;
- using ZB.QueueSys.Common;
- namespace ZB.QueueSys
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
- //处理UI线程异常
- Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
- //处理非UI线程异常
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //数据库类型
- DBHelper.Instance.DBType = int.Parse(ConfigurationManager.AppSettings["DBType"]);
- //数据库连接串
- DBHelper.Instance.ConnectionStr = ConfigurationManager.ConnectionStrings["DBStr"].ConnectionString;
- Application.Run(new MainForm());
- //Application.Run(new QueueForm());
- }
- private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
- LogHelper.Instance.SaveText(str);
- Exception error = e.ExceptionObject as Exception;
- MessageBox.Show(error.Message, "系统异常提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
- {
- string str = GetExceptionMsg(e.Exception, e.ToString());
- LogHelper.Instance.SaveText(str);
- MessageBox.Show(e.Exception.Message, "系统异常提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- /// <summary>
- /// 生成自定义异常消息
- /// </summary>
- /// <param name="ex">异常对象</param>
- /// <param name="backStr">备用异常消息:当ex为null时有效</param>
- /// <returns>异常字符串文本</returns>
- private static string GetExceptionMsg(Exception ex, string backStr)
- {
- StringBuilder sb = new StringBuilder();
- sb.AppendLine("****************************异常文本****************************");
- sb.AppendLine("【出现时间】:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
- if (ex != null)
- {
- sb.AppendLine("【异常类型】:" + ex.GetType().Name);
- sb.AppendLine("【异常信息】:" + ex.Message);
- sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
- sb.AppendLine("【异常方法】:" + ex.TargetSite);
- }
- else
- {
- sb.AppendLine("【未处理异常】:" + backStr);
- }
- sb.AppendLine("***************************************************************");
- return sb.ToString();
- }
- }
- }
C#中全局处理异常方式的更多相关文章
- android中全局异常捕捉
android中全局异常捕捉 只要写代码就会有bug,但是我们要想办法收集到客户的bug.有第三方bugly或者友盟等可以收集.但是,android原生就提供了有关收集异常的api,所以我们来学习一下 ...
- Spring Boot 中全局异常处理器
Spring Boot 中全局异常处理器,就是把错误异常统一处理的方法.等价于Springmvc中的异常处理器. 步骤一:基于前面的springBoot入门小demo修改 步骤二:修改HelloCon ...
- C#中异步编程异常的处理方式
异步编程异常处理 在同步编程中,一旦出现错误就会抛出异常,我们可以使用try-catch来捕捉异常,未被捕获的异常则会不断向上传递,形成一个简单而统一的错误处理机制.但是对于异步编程来说,异常处理一直 ...
- Android中处理崩溃异常
转自:http://my.eoe.cn/817027/archive/17997.html 大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不 ...
- Android中处理崩溃异常和记录日志
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- Android中处理崩溃异常CrashHandler
来源:http://blog.csdn.net/liuhe688/article/details/6584143 大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程 ...
- 【转】Android中处理崩溃异常
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- swoole中退出、异常与错误的处理笔记
关于PHP这方面的知识 可以看 https://www.cnblogs.com/zyf-zhaoyafei/p/6928149.html 进行补课 然后下面记录一下使用swoole的时候需要注意的地方 ...
- spring mvc 全局处理异常
spring框架支持很多种全局处理异常的方式 一.Spring MVC处理异常有4种方式: (1)使用Spring-MVC提供的SimpleMappingExceptionResolver: (2)实 ...
随机推荐
- Oracle自我补充之trunc()函数使用介绍
oracle trunc函数使用介绍 核心提示:oracle trunc函数使用介绍 1.TRUNC(for dates) TRUNC函数为指定元素而截去的日期值. 其具体的语法格式如下: TRUNC ...
- Linux内存压力测试stressapptest
/********************************************************************** * Linux内存压力测试stressapptest * ...
- Gym - 101002K:YATP (树分治+二分+斜率优化)
题意:给定带点权边权的树,定义路径的花费=路径边权和e+起点点权w[s]*终点点权w[t].N<2e5,e,w<1e6: 思路:首先,需要树分治. 然后得到方程dp[i]=min{ dis ...
- dfs——皇后问题(回溯)
#include <iostream> using namespace std; ],b[],c[],d[]; ; dfs(int i) { if(i>n) { sum++; ) { ...
- 基本数据类型,数字int字符串str
基本数据类型 数字 int 字符串 str 布尔值 bool 列表 list 字典 dict 元组 tuple(待续...) 整数 int - 创建 a = 123 a = int(123) - 转换 ...
- 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 ...
- 用flask和长轮询实现对帅哥投票和实时查看票数
flask中的代码 from flask import Flask,request,render_template,redirect,session,jsonify import uuid impor ...
- 100 webhook implementations
转自: https://streamdata.io/blog/100-webhook-implementations/ 很不错的整理 What is the scope of the event-d ...
- deno学习三 官方提供的方便deno 安装方式
早起deno 使用了golang 开发,同时需要protobuf 进行数据的序列化以及反序列化处理 当前的deno 已经使用rust 进行了开发,同时官方提供的安装方式也很方便了,不需要 那么复杂的编 ...
- MySQL中如何实现 select top n
mysql 没有 top n 语法,mysql 用 limit 来实现相关功能,而且功能更加强大. 语法: SELECT * FROM table LIMIT [offset,] rows | row ...