我经常在控制台应用程序中发现未处理的异常。然后它挂起并停止所有进程。我还被迫重新启动程序才能正常工作。
未处理的异常:
我的程序是一个服务器程序。我不希望它停止工作,因为它会影响业务。
我该如何处理这个错误?
最佳答案
在主方法中注册一个全局异常处理程序,如下所示:
//Add handler to handle the exception raised by additional threads
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
然后在CurrentDomain_UnhandledException
方法中处理所有未处理的异常。
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Your logic here, for ex., log the exception somewhere
}
关于c# - 如何在控制台应用程序中捕获未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52716703/