C# RuntimeWrappedException与UnhandledException
这是处理这两种情况的最佳方法吗C# RuntimeWrappedException与UnhandledException,c#,exception,C#,Exception,这是处理这两种情况的最佳方法吗 public static class Program { //http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx //http://social.msdn.microsoft.com/forums/en-US/wpf/thread/dee46bde-9baa-46b6-889c-04e20dd04029
public static class Program
{
//http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
//http://social.msdn.microsoft.com/forums/en-US/wpf/thread/dee46bde-9baa-46b6-889c-04e20dd04029
//http://msdn.microsoft.com/en-us/library/ms404228.aspx?appId=Dev10IDEF1&l=EN-US&k=k%28CS1058%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK&k=VERSION=V4.0%22%29&rd=true
[STAThread]
public static Int32 Main(string[] args)
{
bool unhandledInstalled = false;
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
unhandledInstalled = true;
var app = new App();
app.DispatcherUnhandledException += DispatcherUnhandledException;
return app.Run();
}
catch (SecurityException)
{
// Notify
}
catch (Exception e)
{
var rwe = e as RuntimeWrappedException;
if (rwe != null)
{
object wrappedException = rwe.WrappedException;
MessageBox.Show(wrappedException.ToString());
}
if (unhandledInstalled)
throw;
// No handler has been installed but handle it anyway.
UnhandledException(null, new UnhandledExceptionEventArgs(e, true));
}
return -1;
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs args)
{
MessageBox.Show(args.ExceptionObject.ToString());
Environment.Exit(-1);
}
private static void DispatcherUnhandledException(object sender,
DispatcherUnhandledExceptionEventArgs e)
{
// Do something here
e.Handled = true;
}
}
谢谢大家! Main()方法中的异常处理程序永远不会运行。Dispatcher.UnhandledException处理程序将阻止app.Run()因异常而退出。在事件处理程序中设置e.Handled=true。不要让它保持沉默,让用户知道发生了什么事情
顺便说一句,“unhandledInstalled”变量很奇怪。您安装了一个,它总是正确的。如果代码在try块之前崩溃,那么就没有理由尝试做一些有意义的事情,发生了非常糟糕的事情。Main()方法中的异常处理程序将永远不会运行。Dispatcher.UnhandledException处理程序将阻止app.Run()因异常而退出。在事件处理程序中设置e.Handled=true。不要让它保持沉默,让用户知道发生了什么事情
顺便说一句,“unhandledInstalled”变量很奇怪。您安装了一个,它总是正确的。如果代码在try块之前崩溃,那么就没有理由尝试做一些有意义的事情,一些非常糟糕的事情发生了。我认为我做对了,但是我还无法测试RuntimeWrappedException, 我刚刚发现DispatcherUnhandledException处理应用程序中的所有内容,我认为它严格适用于Dispatcher.BeginInvoke()之类的东西 谢谢,
[STAThread]
public static Int32 Main(string[] args)
{
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
var app = new App();
app.DispatcherUnhandledException += DispatcherUnhandledException;
int run = app.Run();
return run;
}
catch (SecurityException)
{
// Notify & exit
}
catch (Exception e)
{
var rwe = e as RuntimeWrappedException;
if (rwe != null)
{
object wrappedException = rwe.WrappedException;
MessageBox.Show(wrappedException.ToString());
Environment.Exit(-1);
}
throw;
}
return -1;
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject.ToString());
Environment.Exit(-1);
}
private static void DispatcherUnhandledException(object sender,
DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
// Decide if we exit or not
// ...
e.Handled = true;
}
我想我做对了,但我还没能测试RuntimeWrappedException, 我刚刚发现DispatcherUnhandledException处理应用程序中的所有内容,我认为它严格适用于Dispatcher.BeginInvoke()之类的东西 谢谢,
[STAThread]
public static Int32 Main(string[] args)
{
try
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
var app = new App();
app.DispatcherUnhandledException += DispatcherUnhandledException;
int run = app.Run();
return run;
}
catch (SecurityException)
{
// Notify & exit
}
catch (Exception e)
{
var rwe = e as RuntimeWrappedException;
if (rwe != null)
{
object wrappedException = rwe.WrappedException;
MessageBox.Show(wrappedException.ToString());
Environment.Exit(-1);
}
throw;
}
return -1;
}
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show(e.ExceptionObject.ToString());
Environment.Exit(-1);
}
private static void DispatcherUnhandledException(object sender,
DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(e.Exception.ToString());
// Decide if we exit or not
// ...
e.Handled = true;
}
你最好把这类东西贴出来,作为对你原来问题的修正,而不是自己创造答案。除非你打算接受你的答案,并且它与已经提出的答案不同,否则用你的解决方案摘要来修改你的问题是关于堆栈溢出的标准做法。你最好将这类内容作为对原始问题的修改发布,而不是创建自己的答案。除非您计划接受您的答案,并且它与已经提出的答案不同,否则用解决方案的摘要修改您的问题是关于堆栈溢出的标准实践。