Find uncaught exceptions in C# code

2019-01-25 18:15发布

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.

1条回答
淡お忘
2楼-- · 2019-01-25 18:40

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

查看更多
登录 后发表回答