在Windows窗体应用程序中捕获应用程序异常

时间:2021-10-27 20:36:56

Is there anyway to catch expections that is thrown by anywhere in the code? I would like to catch exceptions and handle them in a similar manner rather than writing try catch blocks for each functionality.

无论如何都要捕获代码中任何地方引发的探测?我想捕获异常并以类似的方式处理它们,而不是为每个功能编写try catch块。

5 个解决方案

#1


35  

In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can treat all the exceptions in the same way.

在Windows窗体应用程序中,当在应用程序中的任何位置(在主线程上或在异步调用期间)抛出异常时,您可以通过在Application上注册ThreadException事件来捕获它。通过这种方式,您可以以相同的方式处理所有异常。

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}

#2


21  

I think this is a close as you can get to what you are looking for within a win form application.

我认为这是一个接近,因为你可以在win form应用程序中找到你想要的东西。

http://msdn.microsoft.com/en-us/library/ms157905.aspx

http://msdn.microsoft.com/en-us/library/ms157905.aspx

// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

// Set the unhandled exception mode to force all Windows Forms errors to go through
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event. 
AppDomain.CurrentDomain.UnhandledException +=
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Without doing all of these steps you run the risk of having some exceptions get unhandled.

如果不执行所有这些步骤,您可能会遇到一些未处理的异常风险。

#3


16  

The obvious answer is to put an exception handler at the top of your execution chain.

显而易见的答案是将异常处理程序放在执行链的顶部。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    try
    {
        Application.Run(new YourTopLevelForm());
    }
    catch
    {
        //Some last resort handler unware of the context of the actual exception
    }
}

This will catch any exception that occurs on your main GUI thread. If you also want to globally catch exceptions that occur on all threads you can subscribe to the AppDomain.UnhandledException event and handle there.

这将捕获主GUI线程上发生的任何异常。如果您还想全局捕获在所有线程上发生的异常,您可以订阅AppDomain.UnhandledException事件并在那里处理。

Application.ThreadException +=
    new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)
private static void MyCommonExceptionHandlingMethod(
                                              object sender,
                                              ThreadExceptionEventArgs t)
{
    //Exception handling...
}

Code copied from Charith J's answer

代码复制自Charith J的答案

Now on to the advice ...

现在提出建议......

These options should only be used as a last resort, say, if you want to suppress accidentally uncaught exceptions from presentation to the user. You should be catching sooner whenever possible, when you know somthing about the context of the exception. Even better, you may be able to do something about the problem.

这些选项应该仅作为最后的手段使用,例如,如果您想要禁止从呈现给用户的意外未捕获的异常。当你知道关于异常情况的某些事情时,你应该尽可能早地抓住。更好的是,您可以对此问题采取一些措施。

Structured exception handling might seem like an unecessary overhead which you can work around with a catch all but, it exists because this is not the case. What is more, this work should be done as the code is written, when the developer has the logic fresh in thier mind. Do not be lazy and leave this work for later or for some more profressional developer to pick up.

结构化异常处理可能看起来像是一个不必要的开销,你可以解决所有问题,但它存在,因为事实并非如此。更重要的是,这项工作应该在编写代码时完成,而开发人员的逻辑则是新鲜的。不要懒惰,留下这项工作以供日后使用,或让一些更专业的开发人员接受。

Apologies if you already know and do this.

如果您已经知道并且这样做,请道歉。

#4


4  

See AppDomain.UnhandledException and Application.ThreadException.

请参阅AppDomain.UnhandledException和Application.ThreadException。

#5


3  

You can subscribe to AppDomain.UnhandledException Event

您可以订阅AppDomain.UnhandledException事件

#1


35  

In Windows Forms applications, when an exception is thrown anywhere in the application (on the main thread or during asynchronous calls), you can catch it by registering for the ThreadException event on the Application. In this way you can treat all the exceptions in the same way.

在Windows窗体应用程序中,当在应用程序中的任何位置(在主线程上或在异步调用期间)抛出异常时,您可以通过在Application上注册ThreadException事件来捕获它。通过这种方式,您可以以相同的方式处理所有异常。

Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod);

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}

#2


21  

I think this is a close as you can get to what you are looking for within a win form application.

我认为这是一个接近,因为你可以在win form应用程序中找到你想要的东西。

http://msdn.microsoft.com/en-us/library/ms157905.aspx

http://msdn.microsoft.com/en-us/library/ms157905.aspx

// Add the event handler for handling UI thread exceptions to the event.
Application.ThreadException += new ThreadExceptionEventHandler(ErrorHandlerForm.Form1_UIThreadException);

// Set the unhandled exception mode to force all Windows Forms errors to go through
// our handler.
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling non-UI thread exceptions to the event. 
AppDomain.CurrentDomain.UnhandledException +=
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Without doing all of these steps you run the risk of having some exceptions get unhandled.

如果不执行所有这些步骤,您可能会遇到一些未处理的异常风险。

#3


16  

The obvious answer is to put an exception handler at the top of your execution chain.

显而易见的答案是将异常处理程序放在执行链的顶部。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    try
    {
        Application.Run(new YourTopLevelForm());
    }
    catch
    {
        //Some last resort handler unware of the context of the actual exception
    }
}

This will catch any exception that occurs on your main GUI thread. If you also want to globally catch exceptions that occur on all threads you can subscribe to the AppDomain.UnhandledException event and handle there.

这将捕获主GUI线程上发生的任何异常。如果您还想全局捕获在所有线程上发生的异常,您可以订阅AppDomain.UnhandledException事件并在那里处理。

Application.ThreadException +=
    new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)
private static void MyCommonExceptionHandlingMethod(
                                              object sender,
                                              ThreadExceptionEventArgs t)
{
    //Exception handling...
}

Code copied from Charith J's answer

代码复制自Charith J的答案

Now on to the advice ...

现在提出建议......

These options should only be used as a last resort, say, if you want to suppress accidentally uncaught exceptions from presentation to the user. You should be catching sooner whenever possible, when you know somthing about the context of the exception. Even better, you may be able to do something about the problem.

这些选项应该仅作为最后的手段使用,例如,如果您想要禁止从呈现给用户的意外未捕获的异常。当你知道关于异常情况的某些事情时,你应该尽可能早地抓住。更好的是,您可以对此问题采取一些措施。

Structured exception handling might seem like an unecessary overhead which you can work around with a catch all but, it exists because this is not the case. What is more, this work should be done as the code is written, when the developer has the logic fresh in thier mind. Do not be lazy and leave this work for later or for some more profressional developer to pick up.

结构化异常处理可能看起来像是一个不必要的开销,你可以解决所有问题,但它存在,因为事实并非如此。更重要的是,这项工作应该在编写代码时完成,而开发人员的逻辑则是新鲜的。不要懒惰,留下这项工作以供日后使用,或让一些更专业的开发人员接受。

Apologies if you already know and do this.

如果您已经知道并且这样做,请道歉。

#4


4  

See AppDomain.UnhandledException and Application.ThreadException.

请参阅AppDomain.UnhandledException和Application.ThreadException。

#5


3  

You can subscribe to AppDomain.UnhandledException Event

您可以订阅AppDomain.UnhandledException事件