以下输出来自哪里?
您的程序中有一个未处理的异常。这意味着您允许异常冒泡并从应用程序中溢出,因此dotnet运行时被迫为您处理它。
就像
Console.WriteLine()
我真的建议使用try/catch,这就是它的设计目的。它让你有时间思考应用程序的设计,并允许你在处理应用程序时执行所有自定义逻辑
非常精确的错误
.
John Wu
AppDomain.UnhandledException Event
然而,这或多或少相当于“对整个应用程序进行一次尝试/捕获,然后到此为止”。
文档示例如下:
using System;
public class Example
{
public static void Main()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
try {
throw new Exception("1");
} catch (Exception e) {
Console.WriteLine("Catch clause caught : {0} \n", e.Message);
}
throw new Exception("2");
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception) args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
}
}
// The example displays the following output:
// Catch clause caught : 1
//
// MyHandler caught : 2
// Runtime terminating: True
//
// Unhandled Exception: System.Exception: 2
// at Example.Main()