在不终止应用程序的情况下处理全局异常?

发布于 2024-12-29 15:31:59 字数 255 浏览 0 评论 0原文

我在我的代码中抛出了各种自定义异常。

其中一些是可以恢复的,所以我想要一个全局处理程序来捕获它们,显示警告,然后继续。

我发现 AppDomain.UnhandledException 事件有一个 IsTerminate 参数,但这是只读的。

是否有一些事件或其他方法可以全局捕获异常,仍然可以让您处理它们并继续?

(这是在 Forms 中,但我对 WPF 解决方案也很感兴趣)

I throw various custom exceptions thoughout my code.

Some of these are recoverable from, so I want a global handler to catch them, display a warning, then carry on.

I have found the AppDomain.UnhandledException event which has an IsTerminating argument, but this is read only.

Is there some event or other way to catch exceptions globally that still lets you handle them and carry on?

(This is in Forms, but I'd be interesting in a WPF solution too)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巷雨优美回忆 2025-01-05 15:31:59

您可以使用 global.asax 和

void Application_Error(object sender, EventArgs e)
{
}

事件来捕获页面上未捕获的错误。

在此范围内,您可以使用多种方法,但其中一种方法如下所示:

void Application_Error(object sender, EventArgs e)
{
    Exception opException = Server.GetLastError();

    switch (opException.GetBaseException().GetType().Name)
    {
        case "OptimisticConcurrencyException":
                        //display a friendly message
                    break;
    }


    Server.ClearError();

}

或类似的方法

You could use the global.asax and the

void Application_Error(object sender, EventArgs e)
{
}

Event to catch errors which aren't caught on the page.

Within this, you could use a number of methods, but one such way would be something like this:

void Application_Error(object sender, EventArgs e)
{
    Exception opException = Server.GetLastError();

    switch (opException.GetBaseException().GetType().Name)
    {
        case "OptimisticConcurrencyException":
                        //display a friendly message
                    break;
    }


    Server.ClearError();

}

Or something similar

背叛残局 2025-01-05 15:31:59

尝试类似的操作:

AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;

public static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // You might have to try changing this to your custom exception type and then have overloads or pass it as an object and cast it in the HandleException method
    HandleException(e.ExceptionObject as Exception);
}

private static void HandleException(Exception exception)
{
    if (exception == null) return;

    // Do a check in here against the exceptions that you want to handle or don't want to handle and either display a message, log, quit, etc
}

没有注意到 winforms,我已经很长时间没有做过表单了,所以这是 WPF,可能有效或可能需要调整。

Try something like:

AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;

public static void AppDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    // You might have to try changing this to your custom exception type and then have overloads or pass it as an object and cast it in the HandleException method
    HandleException(e.ExceptionObject as Exception);
}

private static void HandleException(Exception exception)
{
    if (exception == null) return;

    // Do a check in here against the exceptions that you want to handle or don't want to handle and either display a message, log, quit, etc
}

Didn't notice winforms, I haven't done forms in a long time so this is WPF, might work or might need to be tweaked.

桃酥萝莉 2025-01-05 15:31:59

已经找到解决办法了。使用 Application.ThreadException 而不是 AppDomain.UnhandledException。但您还必须告诉它在 UI 线程上也给出异常:

public static void SetupGlobalExceptionHandler()
{
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    Application.ThreadException += OnException;
}

static void OnException(object sender, ThreadExceptionEventArgs args)
{
    MessageBox.Show("There was a problem. Error is:\n\n" + args.Exception.Message);
}

Have found the solution. Use Application.ThreadException instead of AppDomain.UnhandledException. But you must also tell it to give exceptions on the UI thread too:

public static void SetupGlobalExceptionHandler()
{
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
    Application.ThreadException += OnException;
}

static void OnException(object sender, ThreadExceptionEventArgs args)
{
    MessageBox.Show("There was a problem. Error is:\n\n" + args.Exception.Message);
}
舂唻埖巳落 2025-01-05 15:31:59

这是一个旧线程,但 Dispatcher.UnhandledException 事件在其事件参数上有一个 Handled 参数,您可以使用该参数来阻止应用程序关闭。

protected override void OnStartup(StartupEventArgs e)
{
    Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    HandleException(e.Exception);//your handler
    e.Handled = true;
}

This is an old thread, but the Dispatcher.UnhandledException event has a Handled parameter on it's event args that you can use to stop the application from shutting down.

protected override void OnStartup(StartupEventArgs e)
{
    Dispatcher.UnhandledException += Dispatcher_UnhandledException;
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
    HandleException(e.Exception);//your handler
    e.Handled = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文