- ASP.NET Core 2 High Performance(Second Edition)
- James Singleton
- 214字
- 2021-07-08 09:38:58
Exception filters
You can filter exceptions more easily with the when keyword. You no longer need to catch every type of exception that you are interested in and then filter it manually inside the catch block. This is a feature that was already present in VB and F#, so it's nice that C# has finally caught up.
There are some small benefits to this approach. For example, if your filter is not matched, then the exception will still be caught by other catch blocks in the same try statement. You also don't need to remember to rethrow the exception to avoid it from being swallowed. This helps with debugging, as Visual Studio will no longer break as it would when you use throw.
For example, you could check to see whether there is a message in the exception and handle it differently, as shown here:
catch (Exception e) when (e?.Message?.Length > 0)
When this feature was in development, a different keyword (if) was used. So be careful of any old information online.
One thing to keep in mind is that relying on a particular exception message is fragile. If your application is localized, then the message may be in a different language to what you expect. This holds true outside of exception filtering too.
- Python概率統計
- 測試驅動開發:入門、實戰與進階
- Visual FoxPro程序設計教程(第3版)
- 構建移動網站與APP:HTML 5移動開發入門與實戰(跨平臺移動開發叢書)
- Learning AWS Lumberyard Game Development
- NumPy Essentials
- Learning Python Design Patterns(Second Edition)
- 精通Linux(第2版)
- 數據結構案例教程(C/C++版)
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- Access 2010中文版項目教程
- Practical Game Design with Unity and Playmaker
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- JavaScript從入門到精通(視頻實戰版)
- Python Web自動化測試設計與實現