Home >Backend Development >C#.Net Tutorial >C# exception enhancement
introduces a pair of keywords await/async in C#5 to support the new asynchronousProgramming model, making C# asynchronous programming The model is further simplified (APM->EAP->TAP->await/async. The asynchronous programming model in C# is not the Introduction focus of this article. For detailed information, please go here Asynchronous Programming Pattern). Although await/async was introduced in C#5, it has some restrictions. For example, it cannot be used in catch and finally statement blocks. This restriction will no longer apply in C#6.
1 using System; 2 using System.Threading; 3 using System.Threading.Tasks; 4 5 namespace csharp6 6 { 7 internal class Program 8 { 9 private static void Main(string[] args)10 {11 do12 {13 Log(ConsoleColor.White, "caller method begin", true);14 CallerMethod();15 Log(ConsoleColor.White, "caller method end");16 } while (Console.ReadKey().Key != ConsoleKey.Q);17 }18 19 public static async void CallerMethod()20 {21 try22 {23 Log(ConsoleColor.Yellow, "try ", true);24 throw new Exception();25 }26 catch (Exception)27 {28 Log(ConsoleColor.Red, "catch await begin", true);29 await AsyncMethod();30 Log(ConsoleColor.Red, "catch await end");31 }32 finally33 {34 Log(ConsoleColor.Blue, "finally await begin", true);35 await AsyncMethod();36 Log(ConsoleColor.Blue, "finally await end");37 }38 }39 40 private static Task AsyncMethod()41 {42 return Task.Factory.StartNew(() =>43 {44 Log(ConsoleColor.Green, "async method begin");45 Thread.Sleep(1000);46 Log(ConsoleColor.Green, "async method end");47 });48 }49 50 private static void Log(ConsoleColor color, string message, bool newLine = false)51 {52 if (newLine)53 {54 Console.WriteLine();55 }56 Console.ForegroundColor = color;57 Console.WriteLine($"{message,-20} : {Thread.CurrentThread.ManagedThreadId}");58 }59 }60 }
The running results are as follows:
If you are careful, you will find thatasync method begin:6The color of this line is not the green I set, but white, and the order is also out of order; if you run it again, it may be green. This is actually caused by the two lines of code in my Log method (non-thread safe method) being called by multiple threads:
1 Console.ForegroundColor = color;2 Console.WriteLine($"{message,-20} : {Thread.CurrentThread.ManagedThreadId}");
1 [MethodImpl(MethodImplOptions.Synchronized)] 2 private static void Log(ConsoleColor color, string message, bool newLine = false) 3 { 4 if (newLine) 5 { 6 Console.WriteLine(); 7 } 8 Console.ForegroundColor = color; 9 Console.WriteLine($"{message,-20} : {Thread.CurrentThread.ManagedThreadId}");10 }
The code we wrote in CallerMethod was transferred to Move
Next(For more detailed information, please visit a blog of garden friend "Dev_Eric": Advanced article: Using IL as a sword, pointing directly to async/await) (including the await statements in catch and finally ). 2. Exception
Filter1 try { … }2 catch (Exception e) when (filter(e))3 {4 …5 }
. If the expression result is true, enter The current catch statement block. 3. Reference
C# 6.0 await in catch/finally
C# 6.0 Exception filters
http:/ /www.php.cn/
The above is the detailed content of C# exception enhancement. For more information, please follow other related articles on the PHP Chinese website!