C #6.0 (C # vNext): Exception-Handling Improvements
In C #6.0, the new feature wraps two improvements to exception handling:
Async and await can be used in catch block. The async and await introduced in C #5.0 cannot be used for catch block at that time. Now it is enough. Example of exception filtering asynchronous processing:
try{ WebRequest webRequest = WebRequest.Create("http://www.csdn.net"); WebResponse response = await webRequest.GetResponseAsync(); // ...}catch (WebException exception){ await WriteErrorToLog(exception);}
Exception filtering, which allows C # To keep up with Visual Basic. NET and F # in this regard.
Example:
try{ DoSomeHttpRequest();}catch (System.Web.HttpException e) if (e.GetHttpCode() == 400){ WriteLine("Not Found");}catch (System.Web.HttpException e) if (e.GetHttpCode() == 500){ WriteLine("Internal Server Error");}catch{ WriteLine("Generic Error");}
Compare the previous statements:
try{ DoSomeHttpRequest();}catch (System.Web.HttpException e){ switch (e.GetHttpCode()) { case 400: WriteLine("Bad Request"); case 500: WriteLine("Internal Server Error"); default: WriteLine("Generic Error"); }}