我是一个为UI构建WPF应用程序的团队,同时重用了最初在C ++ / OpenGL中开发的大部分核心应用程序(图形处理和3d建模)。我面临的问题是我似乎无法捕获从C ++层抛出的异常。我对WPF应用程序进行了全局错误处理,它确实捕获了所有未处理的C#异常。我需要做类似的事情来捕获C ++异常,但到目前为止我还没有能够让它工作。代码如下。
public partial class App : Application
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// Overrides Application.OnStartup to register the global exception handler
/// </summary>
/// <param name="e"></param>
protected override void OnStartup(StartupEventArgs e)
{
Application.Current.DispatcherUnhandledException +=
new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(Application_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += AppDomain_UnhandledException;
base.OnStartup(e);
}
private void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Logger.Error("Application encountered an unhandled exception", e.ExceptionObject.ToString());
GenericDialog genericDialog = new GenericDialog(OmegaConstant.DialogType.Error,
String.Format("Application has encountered an unhandled exception leaving the application in an unstable state. Error: {0}",
e.ToString()), 900);
genericDialog.ShowDialog();
Logger.Error("Application is shutting down");
Application.Current.Shutdown();
}
/// <summary>
/// Hanndles all uncaught exceptions from C# layer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Application_DispatcherUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
Logger.Error("Application encountered an unhandled exception", e.Exception);
GenericDialog genericDialog = new GenericDialog(OmegaConstant.DialogType.Error,
String.Format("Application has encountered an unhandled exception leaving the application in an unstable state. Error: {0}",
e.Exception.Message), e.Exception, true, 900);
genericDialog.ShowDialog();
e.Handled = true;
Logger.Error("Application is shutting down");
Application.Current.Shutdown();
}
}