This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3070|Reply: 0

[Source] .NET/C# try-catch-when syntax

[Copy link]
Posted on 10/18/2023 8:04:33 PM | | | |
Today, when I was looking at the source code of Serilog.AspNetCore.RequestLoggingMiddleware, I saw catch followed by the when keyword in the method, and then I never used the try-catch-when syntax.



when(C# reference):The hyperlink login is visible.

Use the context keyword when to specify the filter in the following contexts:

  • In the catch clause of the try-catch or try-catch-finally statement.
  • As a case guard in the switch statement.
  • as a case guard in the switch expression.


try-catch-when syntax

You can use the when keyword in the catch clause to specify a condition,This condition must be true for the handler to execute the handler for a particular exception。 The syntax is:

Among them, expr is an expression whose calculation result is a boolean value. If the expression returns true, the exception handler is executed; If false is returned, it is not executed.

true: Enter the catch method to catch the exception.
false: Do not enter the current catch method, if there are other catches, it may enter other catch methods, if there are no other catches, the program will be exceptional.

The test is as follows:



(End)





Previous:The front-end JS converts base64 into a blob and displays the image
Next:OpenWrt Setting Up Local Packages (Source)
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com