全局处理未处理异常

program.cs入口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using GDZLCHECK.Forms;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GDZLCHECK
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

//处理未捕获的异常
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
glExitApp = true;
}
catch (Exception e)
{
MessageBox.Show("请确保安装有ArcGIS和Office,且ArcGIS License已启动");
Application.Exit();

}

}
static bool glExitApp = false;
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("抱歉,您的操作没有能够完成,请再试一次或者联系软件提供商");
Logger logger = LogManager.GetCurrentClassLogger();
logger.Error("CurrentDomain_UnhandledException");
logger.Error("IsTerminating:" + e.IsTerminating.ToString());
logger.Error(e.ExceptionObject.ToString());
}

static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show("抱歉,您的操作没有能够完成,请再试一次或者联系软件提供商");
Logger logger = LogManager.GetCurrentClassLogger();
logger.Error("Application_ThreadException:"+e.Exception.Message);
logger.Error(e.Exception);
//throw new NotImplementedException();
}
}
}