Some. NET skills on multi-thread Exception Handling

Source: Internet
Author: User

Multi-threaded Environment
In our product SE, there are two main categories of multithreading, one is to actively initiate multithreading through ThreadPool or new Thread, and the other is Socket communication callback.
Multi-thread exception capture
For general exception handling, we only need to simply include the statements that may cause errors in try/catch statements. I used this method to capture exceptions in multiple threads. The result is not as follows:
Copy codeThe Code is as follows:
Public static void Main ()
{
Try
{
New Thread (Go). Start ();
}
Catch (Exception ex)
{
// It will never be executed here!
Console. WriteLine ("Exception! ");
}
}
Private static void Go ()
{
Throw null;
}

The correct method should be to capture exceptions in the new thread entry method Go:
Copy codeThe Code is as follows:
Public static void Main ()
{
New Thread (Go). Start ();
}
Private static void Go ()
{
Try
{
...
Throw null; // This exception will be caught
...
}
Catch (Exception ex)
{
// Exception logging, or notifying other threads of exceptions
...
}
}

The above correct practice comes from the Exception Handling section in Threading in C #. This article covers all aspects of. NET multithreading and is the most comprehensive and best article I have seen.

The method for correctly capturing multithreaded exceptions is found. Next we will naturally think: Is this required for every thread entry method?

In addition, let's look at the Exception Handling description in the Threading in C # section: From. NET 2.0, any unhandled exceptions on a thread will cause the entire application to close. Therefore, the try/catch statement must be used in each thread entry method, at least in the product application, this prevents the application from shutting down the entire application due to unexpected code.

If you only write down the exception information and do not care about the application's abnormal shutdown, there are two ways to do this:

1. For Windows Form programs, there is a global exception handling event: Application. ThreadException;

2. for all. NET programs, there is also a lower-level global exception handling event: AppDomain. UnhandledException;
Higher requirements
We can simply record error logs by handling global exceptions. If you do not interrupt the application, you can also capture exceptions and record exceptions in each thread entry method. Is there a way to do this: capture exceptions without interrupting the application, and capture exceptions as easily as global exception handling events?
This can be done at least for newly created threads:
Copy codeThe Code is as follows:
Public static class ThreadExecutor
{
Public static bool Execute (System. Threading. WaitCallback callback, object state)
{
Try
{
Return System. Threading. ThreadPool. QueueUserWorkItem (callback, state );
}
Catch (Exception e)
{
// Log the exception
}
Return false;
}
}

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.