Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find uncaught exceptions in C# code

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.

like image 809
Korexio Avatar asked Oct 12 '11 09:10

Korexio


People also ask

What is uncaught exception in C?

If an exception is not caught, it is intercepted by a function called the uncaught exception handler. The uncaught exception handler always causes the program to exit but may perform some task before this happens.

What are uncaught exceptions give examples?

CWE 248-Uncaught Exception occurs when an exception is not caught by a programming construct or by the programmer, it results in an uncaught exception. In Java, for example, this would be an unhandled exception that would terminate the program.

How do you handle uncaught exceptions?

It allow us to handle the exception use the keywords like try, catch, finally, throw, and throws. When an uncaught exception occurs, the JVM calls a special private method known dispatchUncaughtException( ), on the Thread class in which the exception occurs and terminates the thread.

What is an unhandled exception?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions. Learn about the definition and examples of unhandled exceptions, and explore programming and exception handlers. Updated: 01/04/2022.


1 Answers

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

like image 190
Michael Stum Avatar answered Sep 28 '22 02:09

Michael Stum