The WPF program does not capture exceptions.

Source: Internet
Author: User

The WPF program does not capture exceptions.

It is difficult to avoid the Bug. No one can guarantee that my program will never have a BUG;

 

Suddenly, I took over a very large project and ended unexpectedly during the project running. The exception was very difficult to reproduce. I still couldn't find the BUG code, and there were too many codes.

At this time, you need to record the bugs in the program and know where the bugs are, so that you can solve them in the next update.

Add all the code to try catch? Then you get pregnant.

 

Uncaptured exception handling:

I. C # provides the DispatcherUnhandledException event in the Application class to handle exceptions not captured on the UI thread.

App. Current. DispatcherUnhandledException + = App_DispatcherUnhandledException;
Void App_DispatcherUnhandledException (object sender, System. Windows. Threading. DispatcherUnhandledExceptionEventArgs e)
{
// E. Exception
// E. Handled: whether the exception event has been Handled
}

However, DispatcherUnhandledException cannot catch exceptions on non-UI threads.

 

II. C # provides the UnhandledException event in the AppDomain class to capture exceptions in the application domain.

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e){    MessageBox.Show("Unhandle");//MessageBox.Show((e.ExceptionObject == null ? "Null" : e.ExceptionObject.GetType().Name),    //    "CurrentDomain_UnhandledException:");}

In this way, you can capture exceptions in the application domain, including UI threads and non-UI thread exceptions. If you need to create an application domain, create the application, and then bind the event.

However, the AppDomain. UnhandledException event does not provide a termination exception. That is to say, if an exception occurs and you do not capture it, let us know, but the program has to end.

 

Here we should mention the officially encapsulated Delegate. BeginInvoke ();

"Known:" An exception occurred in the asynchronous function and not handled:

"Solution :"

1. The Delegate. EndInvoke () function is not called:

Terminate execution. The program will not be hung up, and an exception will be discarded. This is a hidden BUG that is hard to find.

The AppDomain. UnhandledException event does not notify you of exceptions. Pay special attention to this.

public void Hello(){    World();    Load();}public void World(){
throw new Exception();
}public void Load(){ MessageBox.Show("Suc");}var action = new Action(Hello);action.BeginInvoke(null, null);

For example, if an exception occurs in the World () method, the thread will terminate immediately, the Load () function will not be executed, and the program will not terminate.

If a function is not executed, it may cause serious exceptions and is difficult to find.

 

2. The Delegate. EndInvoke () function is called:

An exception is thrown and the program fails.

The AppDomain. UnhandledException event notifies you of an exception.

 

There may be a need here. What if I want to handle exceptions in the AppDomain. UnhandledException event? The program is too disgusting.

Let's continue to expand this framework.

Small solution to access UI exceptions in non-UI threads

/// <Summary> /// delegate extension class /// </summary> public static class DelegateExtension {
/// <Summary> /// asynchronous call that can be caught exceptions /// </summary> /// <param name = "dele"> </param> public static void UnsafeBeginInvoke (this Delegate dele) {var action = new Action () => {dele. dynamicInvoke () ;}); action. beginInvoke (new AsyncCallback (ar) =>{ try {action. endInvoke (ar);} catch (Exception ex) {App. current. dispatcher. beginInvoke (new Action () =>{ throw ex ;})) ;}), null );}}

It's a bit nondescribable. Push the exception to the UI thread and let the Application. DispatcherUnhandledException event handle it.

 

Others: C # provides the UnobservedTaskException event in the TaskScheduler class to capture asynchronous Task exceptions.

 

Please comment, please speak out, please ^ v ^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.