user2970840
user2970840

Reputation: 1361

How to detect unhandled exceptions in Visual Studio?

I'm developing an app and in some parts there are unhandled exceptions. I want to catch them in the caller methods and solve them.

putting a general handler is not good for my case. I want to ignore some exceptions and let the app continue working.

Is there any tool or plugin in Visual Studio for this purpose?

Update: to clarify my question: there are lots of method calls without using try catch, I want to know where are these, automatically, without checking all line of codes

Upvotes: 2

Views: 1851

Answers (3)

piedar
piedar

Reputation: 2731

Unlike Java, C# does not have checked exceptions (whether this is a good thing is debated; I think it is). I see a few options to figure out which methods throw what:

  1. Hope the author documented them with /// <exception cref="SomeException">Some explanation</exception> so they'll show up in IntelliSense. All the framework code has fully documented exceptions.

  2. Examine the source or decompiled IL yourself.

  3. Use a utility like Exception Reflector to inspect possible exceptions.

Upvotes: 1

Wombelite
Wombelite

Reputation: 303

The initial comments make some very valid points but if you still want to go down this route you can try the following.

If you know the exceptions that you want to ignore i would suggest adding those to your try catch Consider the following

        try
        {
               //Run Code here
        }
        catch (ArgumentNullException ane)
        {
             // ignore exception here
        }
        catch (AggregateException ae)
        {
             //ignore exception here
        }
        catch (Exception genericException)
        { 
          // this must be last to catch unhandled exceptions
        }

Actually after re-reading your initial post my answer is not entirely appropriate, it means you have to modify your code and enclose your method calls in the try catch..

Upvotes: 0

delliottg
delliottg

Reputation: 4150

The simplest answer is to use a try-catch statement. You wrap the code you want to run in the try, and set the catches below the try. Any exceptions can be handled in the catches, and you can allow the program to continue to run. Your catches should be ordered from the most specific to the most generic last.

try
    {
         ...code to be run
    }
catch(Exception ex)
    {
        \\handle your exceptions here, you can add as many catches as you need.
        Console.WriteLine(ex);
    }

Read more here on MSDN for try-catch

Upvotes: 1

Related Questions