- 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.
- Learning Python Web Penetration Testing
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- PLC編程及應(yīng)用實(shí)戰(zhàn)
- Java性能權(quán)威指南(第2版)
- PhoneGap Mobile Application Development Cookbook
- 快速念咒:MySQL入門指南與進(jìn)階實(shí)戰(zhàn)
- 從零開始學(xué)Linux編程
- Python從入門到精通
- PyQt編程快速上手
- Node.js從入門到精通
- C語言程序設(shè)計(jì)
- 程序員的成長課
- Python預(yù)測分析與機(jī)器學(xué)習(xí)
- Offer來了:Java面試核心知識點(diǎn)精講(框架篇)
- 零基礎(chǔ)學(xué)編程系列(全5冊)