58

Is there any way to find out what exceptions might be thrown by any method in .NET code? Ideally I want to see what might be thrown and choose which ones I want to handle. I guess I want the information you'd get from the throws clause in java.

The situation is I'm doing a linq query on an xml document from the network and want to know what could go wrong. I could open up the assembly in reflector and have a look but I thought there might be an easier way.

Helephant
  • 16,738
  • 8
  • 39
  • 36

4 Answers4

44

.NET does not have enforced ("checked") exceptions like java. The intellisense might show this information, if the developer has added a /// <exception.../> block - but ultimately more exceptions can happen than you expect (OutOfMemoryException, ThreadAbortException, TypeLoadException, etc can all happen fairly unpredictably).

In general, you should have an idea of what things are likely to go wrong, and which ones you can actually do something useful about. In most cases, the correct behaviour is to let the exception bubble up (just running any "finally" code to release resources).

Eric Lippert has a good blog on this subject here.

Nomnom
  • 4,193
  • 7
  • 27
  • 37
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
8

I think that Exception hunter can provide this information however it costs money...

reshefm
  • 6,017
  • 8
  • 36
  • 40
  • 2
    This software has been discontinued. – froeschli Mar 27 '12 at 11:12
  • 5
    This is no comparison to Exception Hunter but if you are looking for a free way to do this, I wrote a quick way to get this info here - https://github.com/stevesheldon/ExceptionReflector. Feel free to fork/update if it doesn't meet your needs. – Steve Sheldon Dec 31 '12 at 19:41
3

After reading another article about this on StackOverflow, I built on top of that other answer to write a tool to do this, you can get the source code from GitHub here:

Exception Reflector

you can also read more here:

http://steves-rv-travels.com/archives/167

Steve Sheldon
  • 6,421
  • 3
  • 31
  • 35
  • I tried this on a library I wrote that calls System.Net.Http, and I don't see those exceptions in the list. Is it because it's an async call, and wrapped in a AggregateException? I'm also using .NET 4.5, maybe that's the culprit. – aoetalks May 02 '16 at 20:06
0

As long as you're using BCL classes, they are all completely documented and Intellisense therefore displays any exception a method can throw. Other than that (and reading the docs), there is no way, I think.

OregonGhost
  • 23,359
  • 7
  • 71
  • 108
  • 1
    There are many exceptions that aren't documented because they can't be predicted; and even then you can't *fully* trust the intellisense to be up to date. Btw, the downvote wasn't from me. – Marc Gravell Nov 05 '08 at 10:14
  • 1
    "Many exceptions" like TypeLoadException that you mention might come from CLR itself, technically speaking, or even from CPU. I don't think anyone is interested in anticipating and catching those. The .NET SDK on the other hand lists exceptions that can be thrown by BCL methods... – liggett78 Dec 21 '08 at 18:31
  • 1
    and those are normally what you're actually interested in. – liggett78 Dec 21 '08 at 18:32