5

I'm wondering if there is a tool to find uncaught exceptions in C# using static code analysis? Basically I want to select a methodA() and want a list of all exceptions thrown by methodA() and all methods called by methodA(). I tried ReSharper + Agent Johnson and AtomineerUtils, both fail this simple task.

Here's my example code:

public class Rectangle
{
    public int Width { get; set; }
    public int Height { get; set; }

    public int Area()
    {
        CheckProperties();
        long x = Width * Height;
        if (x > 10)
            throw new ArgumentOutOfRangeException();
        return (int) x;
    }

    private void CheckProperties()
    {
        if (Width < 0 || Height < 0)
            throw new InvalidOperationException();
    }
}

The tool should be able to tell me (in any form) that method Area() will throw ArgumentOutOfRangeException or InvalidOperationException.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Korexio
  • 483
  • 1
  • 7
  • 19
  • 4
    I see what you want and where you are trying to go but still, consider that in many cases it is ok to throw exceptions from the code, the fact is that every time you use those methods from your actual consuming classes you should catch there, it's probably 100% fine that Area throws something and would not need a catch there, but who calls Rectangle.Area should... – Davide Piras Oct 12 '11 at 09:30
  • @Davide Piras: I understand your point and agree with you, but consider your are using a large third-party library with really bad documentation - finding uncaught (or let's call them "possibly thrown") exceptions would really ease debugging & development. – Korexio Oct 12 '11 at 09:38

1 Answers1

7

I used an R# addin once that did that in the IDE - Exceptional. Bad idea, turns out that it complains about every single string.Format call and similar common cases that may indeed throw, but that won't cause issues.

Decide for yourself if it's worth it: https://github.com/CSharpAnalyzers/ExceptionalReSharper

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
  • When I use a simple program "Console.WriteLine(new Rectangle().Area());" with the class given above, Exceptional only gives me an error for Console.WriteLine(...) (System.IO.IOException uncaught) but not for the call to Area()... The other way round this would be useful, but this way it's no improvement at all... – Korexio Oct 12 '11 at 10:31
  • 1
    Never mind, found the problem: I did not fix the problem in the Area() method - so Exceptional ignored the call. Adding documentation to Area() lead to a warning in Main(...). – Korexio Oct 12 '11 at 10:40
  • Without the issue described above (http://exceptionalplugin.codeplex.com/workitem/9632) Exceptional seems like a useful tool. Still other tool suggestions are welcome! – Korexio Oct 12 '11 at 11:05
  • 1
    The extension is now available for R# 8.2 with lots of improvements – Rico Suter Oct 22 '14 at 21:42