我想處理 WPF 應用程式中的所有意外錯誤。當我查看時,對于不同的情況有不同的事件來捕獲和控制錯誤。我也在應用程式構造方法中添加了這些
public App()
{
AppDomain.CurrentDomain.FirstChanceException = OnFirstChanceException;
AppDomain.CurrentDomain.UnhandledException = OnUnhandledException;
Dispatcher.UnhandledException = OnDispatcherUnhandledException;
TaskScheduler.UnobservedTaskException = OnUnobservedTaskException;
}
但是,我嘗試使用它來檢查處理,但它們沒有用。是什么原因?
private void btnStart_Click(object sender, RoutedEventArgs e) //2021112242
{
throw new StackOverflowException();
}
uj5u.com熱心網友回復:
每個例外事件都有自己的回呼方法和事件物件
// Dispatcher.UnhandledException
private static void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
}
// TaskScheduler.UnobservedTaskException
private static void OnTaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
}
// AppDomain.CurrentDomain.UnhandledException
private static void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
// AppDomain.CurrentDomain.FirstChanceException
private static void OnCurrentDomainUnhandledException(object sender, FirstChanceExceptionEventArgs e)
{
}
uj5u.com熱心網友回復:
App對此有一個名為DispatcherUnhandledException. 這在大多數情況下都有效,但是當從單獨的執行緒拋出例外時,它會變得有點麻煩。
public partial class App
{
public App()
{
// Globally handle errors/exceptions for a friendly close.
DispatcherUnhandledException = DispatcherOnUnhandledException;
}
private void DispatcherOnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
try
{
e.Handled = true;
e.Exception.Display("An unhandled exception occurred, the application will now close.");
e.Exception.Log(); // log the problem.
Shutdown(-1); // assume not recoverable.
}
catch
{
Shutdown(-1);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422333.html
標籤:
