很多时候一些未知的错误报错很多信息, 一点不优雅, 所以需要简单的做个全局异常捕获
[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
using TicketUserClient.Utils;
namespace ChaBai
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </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 ChaBaiClientForm());
}
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
// 处理线程异常
HandleException(e.Exception);
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// 处理未处理的异常
if (e.ExceptionObject is Exception exception)
{
HandleException(exception);
}
}
private static void HandleException(Exception exception)
{
// 在这里执行自定义的异常处理逻辑
MessageBox.Show($"发生异常: {exception.Message}", "系统故障 ( 请截图发给技术处理 ) ", MessageBoxButtons.OK,
MessageBoxIcon.Error);
// 退出应用程序
Environment.Exit(0);
}
}
}
|