001*_*010 2 c# error-handling runtime-error visual-studio-2010 visual-studio
我有一个ErrorRecorder应用程序,它打印错误报告,并询问用户是否要将该报告发送给我.
然后,我有主应用程序.如果发生错误,它会将错误报告写入文件,并要求ErrorRecorder打开该文件以向用户显示错误报告.
所以我使用Try/Catch捕获了大部分错误.
但是,如果发生完全意外的错误并关闭我的程序,该怎么办?
有没有像全局/覆盖方法或类似的东西,它告诉程序"在关闭之前,如果发生意外错误,请调用"ErrorRecorderView()"方法"
我认为这就是你所追求的 - 你可以在appdomain级别处理异常 - 即整个程序.
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();
}
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
5626 次 |
最近记录: |