157 recommendations for writing high-quality code to improve C # programs--Recommendations for exception handling in 86:parallel

Source: Internet
Author: User

Recommended exception Handling in 86:parallel

Recommendation 85 illustrates how to handle exceptions in a task. Because the Start method of the task is started asynchronously, we need additional techniques to complete the exception handling. Parallel is relatively simple, because the caller line of parallel routines wait until all the tasks are complete before continuing their work. In simple terms, it has the characteristics of synchronization, so the following code can be implemented to wrap the concurrency exception into the main thread:

Static voidMain (string[] args) {      Try      {          varParallelexceptions =NewConcurrentqueue<exception>(); Parallel.For (0,1, (i) = =        {              Try              {                  Throw NewInvalidOperationException ("exceptions that occur in parallel tasks"); }              Catch(Exception e) {parallelexceptions.enqueue (e); }              if(Parallelexceptions.count >0)                  Throw Newaggregateexception (parallelexceptions);      }); }      Catch(aggregateexception err) {foreach(Exception IteminchErr. InnerExceptions) {Console.WriteLine ("Exception type: {0}{1} from:{2}{3} Exception Content: {4}", item. Innerexception.gettype (),Environment.NewLine, item. Innerexception.source, Environment.NewLine, item.          Innerexception.message); }} Console.WriteLine ("The main thread ends immediately");  Console.readkey (); } 

The output of this piece of code is:
Exception type: System.InvalidOperationException
From: ConsoleApplication2
Exception content: Exceptions that occur in parallel tasks
The main thread ends immediately


In parallel exception handling, we used a thread-safe generic collection concurrentqueue<t> to handle the collection thread security issues that might be encountered in concurrency (see recommendation 22: Ensure thread safety for collections).

Turn from: 157 recommendations for writing high-quality code to improve C # programs Minjia

157 recommendations for writing high-quality code to improve C # programs--Recommendations for exception handling in 86:parallel

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.