2

I'm looking for a tools, to check if "try-catch" is used properly in codes.

E.g. I have a method:

/// <exception cref="NotImplementedException">Not implemented</exception>
public void MethodThrowException()
{
  throw new NotImplementedException();
}

I have some calls of this method:

 1 void TestA()
 2 {
 3   MethodThrowException();
 4 }

 5 void TestB()
 6 {
 7   try
 8   {
 9     MethodThrowException();
10   }
11   catch
12   {
13   }
14 }

15 void TestC()
16 {
17   try
18   {
19     MethodThrowException();
20   }
21   catch
22   {
23     throw;
24   }
25 }

26 void Main()
27 {
28   TestA();
29   TestB();
30   TestC();

31   try
32   {
33     TestA();
34     TestB();
35     TestC();
36   }
37   catch
38   {
39   }
40 }

Now I let the tool to inspect for MethodThrowException

I wish to get following result:

File xxx, Method TestA(), Line 3, no try-catch.

File xxx, Method TestB(), Line 9, catch exception.

File xxx, Method TestC(), Line 23, re-throw.

File xxx, Method Main(), Line 28, no try-catch.

File xxx, Method Main(), Line 30, re-throw without try.

File xxx, Method Main(), Line 33, catch exception.

File xxx, Method Main(), Line 35, catch exception.

I also want the tool can generate a report for a project o solution, which looks like:

File xxx, Method "MethodThrowException()", 2 calls is not surrounded with try-catch.

(other methods)

Is there any existing tools or plugins can achieve this?

3
  • Welcome aboard. Great question! I am just hoping that you will get an answer that also works for other languages :-) Dec 6 '19 at 8:55
  • 1
    "Is there any existing tools or plugins can achieve this?" - plugins for what? Dec 6 '19 at 8:56
  • In general, aiming for 100% "try-catch" coverage is not a good goal. Error handling shouldn't need to be everywhere, but only when you can do something meaningful with an exception. More often than not, it's simply better to leave an exception unhandled and let the caller deal with it.
    – Alejandro
    Aug 25 '21 at 13:22
0

Resharper can do some of the stuff you mentioned. No tool will tell you missing try catch this is the developer's responsibility to make sure this is placed in the proper places of the code.

ReSharper can help with missing catch or rethrow etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.