捕捉完全意外的错误 [英] Catch completely unexpected error
问题描述
我有一个ErrorRecorder应用程序,它打印出错误报告,如果用户希望该报告发送给我问。
I have a ErrorRecorder App, which prints the error report out and asks if the user wants to send that report to me.
然后,我主要的应用程序。如果发生错误,它写入错误报告文件,并要求ErrorRecorder打开该文件以显示用户的错误报告。
Then, I have the main app. If an error occurs, It writes the error report to a file and asks ErrorRecorder to open that file to show user the error report.
所以我抓住了我的大部分错误使用try / catch语句。
So I am catching most of my errors using Try/Catch.
然而,如果所发生的错误,这是完全出乎意料,并关闭我的程序。
However, what if an error occurs that was completely unexpected and it shuts down my program.
有像全球/重写方法或那样的东西,这告诉程序关停如果发生意外错误之前,称之为ErrorRecorderView()方法
Is there like an Global/Override method or something of that kind, that tells the program "Before shutting down if an unexpected error occurs, call the "ErrorRecorderView()" Method"
推荐答案
我认为这是你追求的 - 你可以在应用程序域级别处理异常 - 即在整个程序结果
的 http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx
i think this is what you're after - you can handle exceptions at the appdomain level - i.e. across the whole program.
http://msdn.microsoft.com/en-GB/library/system.appdomain.unhandledexception.aspx
using System;
using System.Security.Permissions;
public class Test
{
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Example()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
try
{
throw new Exception("1");
}
catch (Exception e)
{
Console.WriteLine("Catch clause caught : " + e.Message);
}
throw new Exception("2");
// Output:
// Catch clause caught : 1
// MyHandler caught : 2
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception e = (Exception)args.ExceptionObject;
Console.WriteLine("MyHandler caught : " + e.Message);
}
public static void Main()
{
Example();
}
}
}
这篇关于捕捉完全意外的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!