Loading... 很多时候一些未知的错误报错很多信息, 一点不优雅, 所以需要简单的做个全局异常捕获 ```csharp using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Windows.Forms; using UserClient.Utils; // time: 2019-03-02 // author: chabai // info: 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); // ------------------------------------ // - 将异常信息,保存到log/error.log文件中 // ------------------------------------ Util.LogException(exception); // 退出应用程序 Environment.Exit(0); } } } ``` 最后修改:2023 年 08 月 08 日 © 允许规范转载 打赏 赞赏作者 微信 赞 0 如果觉得我的文章对你有用,请随意赞赏