Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to automatically see which functions can potentially return exception in c#

nothing more frustrating than to see your code crash in the debugger on a method which exceptionned and you didn't try/catch it.

Is there an easy way to scan through your sources and tag all functions which can potentially throw exceptions?

Does the build in visual assist have some hidden option to colour these functions in a specific color?

thanks

R

like image 236
Toad Avatar asked May 27 '09 14:05

Toad


People also ask

How do you make sure that your code can handle different kinds of error situations?

Take advantage of language-specific semantics and represent when something exceptional has happened. Exceptions are thrown and caught so the code can recover and handle the situation and not enter an error state. Exceptions can be thrown and caught so the application can recover or continue gracefully.

Why we use try and catch in PHP?

PHP supports using multiple catch blocks within try catch. This allows us to customize our code based on the type of exception that was thrown. This is useful for customizing how you display an error message to a user, or if you should potentially retry something that failed the first time.

Why do we need to handle exceptions?

Exception handling is the process of responding to unwanted or unexpected events when a computer program runs. Exception handling deals with these events to avoid the program or system crashing, and without this process, exceptions would disrupt the normal operation of a program.


2 Answers

All code but the most trivial could throw exceptions (out of memory, at the very least). You're probably better off writing your code defensively, with at least a global try/catch rather than trying to micromanage which sections of code will or won't throw exceptions.

like image 143
Clyde Avatar answered Oct 05 '22 13:10

Clyde


No, there is no way to automatically do this nor is there a good way to get a list of all possible exceptions thrown by a method. Here are a few reasons why

  1. Consider implicitly thrown exceptions such as StackOverflowException can be thrown at any time from any method. You must assume that any method in the CLR can throw these types of exceptions
  2. Reflection and / or delegates can hide the actual code being called in a particular method so you cannot inspect all possible code paths of a method.
  3. This would require inspecting IL vs. metadata.
  4. In .Net there is no requirement to document exceptions that are explicitly thrown by an API
like image 35
JaredPar Avatar answered Oct 05 '22 13:10

JaredPar