-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
try-catch and using statements in exception handling #31951
Comments
These are generally good suggestions @bitm0de I'm not convinced that |
You mention that it is not the best option, although no alternative suggestion is made. The current example provides no explanation about what would cause an IndexOutOfRangeException, or whether it's the most appropriate way to handle the underlying cause of the exception rather than preventing it. Thus, it does seem odd to suggest the need for listing the causes for a SocketException, even if it is arguably a better use of a try-catch. |
Fixes dotnet#31951 : Rewrite the exception example so that it's still obvious what can fail, but couldn't be easily anticipated before making the computation.
It says, "Use a try-catch statement for most exception handling" and proceeds to show an example where the exception could be avoided by doing bounds checks. I don't think this should be a recommended coding convention since exception-based programming is more expensive than mitigating exceptions that can be prevented.
If anything, this should demonstrate catching an exception that you can't prevent (i.e. SocketException in some cases), and not catching the System.Exception type but the most relevant derived exception as a best practice.
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
Associated WorkItem - 123090
The text was updated successfully, but these errors were encountered: