Is there a way to make Visual Studio tell me which parts of my code may throw exceptions? Something like the compiler warnings that can be suppressed, but instead it warns me that some line may throw an exception if it isn't surrounded by a try
and catch
.

- 874
- 1
- 8
- 25
-
.NET doesn't have checked exceptions the way that Java does. And sometimes you don't want a try/catch around something because there's no way you can possibly handle any of the exceptions you could expect. The reality is that you'd be suppressing warnings all over the place, far more than you realize. That would be too much noise in your code. – madreflection Feb 18 '20 at 05:55
-
Pretty much every single line can throw exception(s)... Including class/method declarations (also missing types not necessary will be reported on such lines)... If you can narrow down what exactly you want to achieve community may suggest something... (I'm pretty sure there no exact feature you are looking for as it is not exactly useful) – Alexei Levenkov Feb 18 '20 at 05:55
-
You probably need to look into Static Code Analysis tools (SCA). Some tools are out there, some more expensive than others. By using them, you learn by fixing and applying preventive programming. – DRapp Feb 18 '20 at 05:55
-
@MichaelRandall this isn't purely about code, this is more about a functionality in Visual Studio. Visual Studio can sometimes warn the programmer about some "errors" or things that are not recommended to be done in code. What I wish to know is that if there's such a functionality that would tell me something like: "This method may throw an exception, consider surrounding it in a try / catch block". – Washington A. Ramos Feb 18 '20 at 05:56
-
@WashingtonA.Ramos There is no *Warning, your code might throw exception* error handling done in visual studio. You can enable checks where you can stop the debugger at any and all exceptions but you have to know your code. You can use Try / catch but if you write a throw statement in your catch block, visual studio will execute and throw an exception. – Jawad Feb 18 '20 at 06:00
-
@Jawad I don't want Visual Studio (or C#) to enforce me, just silently warn me that *that* line *may* throw an Exception, just like it _warns_ me if I break a naming convention - it won't force me to change the name, just tell me it's not recommended; and I can even suppress that through editor's configuration. Anyway, I think the final conclusion is: there's no such thing built-in. – Washington A. Ramos Feb 18 '20 at 06:04
-
Should I add, not lines like `var x = 1 / 0;` (which if I remember correctly won't even compile), rather lines that call methods that are known to be possible to throw exceptions, like I/O operations. – Washington A. Ramos Feb 18 '20 at 06:05
-
@WashingtonA.Ramos Frankly, there are a million ways an exception can be thrown. `x = 1/0` is the easiest one to cover but there is no way you can have an IDE do the unit testing for you before the execution to show you the possible NRE or Permissions or any of the other exceptions that can be thrown. – Jawad Feb 18 '20 at 06:08
-
@Jawad VS actually kind of can. For example, should I have a line calling the method `OpenStreamForReadAsync` (System.IO.WindowsRuntimeStorageExtensions) and hover my mouse over it, Visual Studio will tell me it doesn't have any exceptions. On the other hand, if I hover over a line that calls `ComputeHash` (System.Security.Cryptography.HashAlgorithm), it'll tell me it may throw an `ArgumentNullException` or a `ObjectDisposedException`. From this, it shouldn't be hard to pop a warning saying that a call to the second one may throw an Exception. This is actually where my question came from. – Washington A. Ramos Feb 18 '20 at 06:14
-
It doesn’t address the entirety of your question, but as part of C# 8’s [(non-)nullable reference types](https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references), the compiler implements flow analysis to help detect potential null references at compile time. That doesn’t address other types of possibly exceptions, of course, but it’s a good place to start. – Jeremy Caney Feb 18 '20 at 06:17
-
there is a difference in runtime exceptions and compiler exceptions. ArgumentNullExceptions are thrown when you are passing a null to a method that requires a value. ObjectDisposedException is also a compile time error. Runtime errors are NullReferenceExceptions, IndexOutOfRange etc that are not accessible within VS unless you run the code. – Jawad Feb 18 '20 at 06:17
-
*"it'll tell me it may throw an `ArgumentNullException` or a `ObjectDisposedException`"* - it can do that because of `exception` tags in the XML documentation, e.g. `
`. – madreflection Feb 18 '20 at 06:22is null. -
@madreflection those are the ones I'm looking for. Should I let one of those out of a `try / catch` block, have Visual Studio tell me it. I don't want deep code analysis, just check if the XML documentation states that that method may throw an exception - if it may, warn me after compilation. – Washington A. Ramos Feb 18 '20 at 06:25
-
So, to conclude - there's no such a thing built in on Visual Studio that does that. Thanks, that was the question. – Washington A. Ramos Feb 18 '20 at 06:27
-
1Feel free to write a code analyzer to do that. You should be aware, though, that they're often forgotten by even the most careful of developers, so you're at the mercy of whoever has written the XML documentation for the libraries you're using. – madreflection Feb 18 '20 at 06:28
2 Answers
No, there is no such functionality in VS.
C# (and .Net in general) does not support any kind of annotation to specify "method throws an interesting exception" (unlike Java, see Why are Exceptions not Checked in .NET?). As result there is no way to have general tool that will let you know that some "interesting" exception ( IOException
as you've suggested) can be thrown from any given method.
In some cases VS/C# compiler can help. I.e. in C# 8 NRE actually has partial special treatment - "nullable reference type" which lets compiler to know if code expects null
or not and warn at compile time if null
passed where it is not supposed to.
Note: XML documentation (which is mentioned in comments as source of intellisense tooltips) is not part of code and there is no requirements for it to be correct/complete. Wast majority of the code does not have any useful XML documentation and documentation is not necessary available at compile time. So while it may serve as basis for some tool I don't believe VS is using it for any code highlighting.

- 98,904
- 14
- 127
- 179
ReSharper usually does that. It adds some warnings for possible exceptions. If you don't want to use it search for similar products.

- 173
- 5