Finding out what exceptions a method might throw in C#
Asked Answered
S

4

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.

Sublet answered 5/11, 2008 at 10:7 Comment(0)
V
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.

Viewpoint answered 5/11, 2008 at 10:10 Comment(3)
Thanks. The link to Eric Lippert's blog was really helpful too. :)Sublet
Link to the article is broken, current working link: learn.microsoft.com/en-us/archive/blogs/ericlippert/…Armhole
@RemcoRos I updated the link in the post, thanks!Snooty
I
8

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

Incoercible answered 5/11, 2008 at 13:45 Comment(2)
This software has been discontinued.Guanine
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 - github.com/stevesheldon/ExceptionReflector. Feel free to fork/update if it doesn't meet your needs.Deeply
D
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

Deeply answered 31/12, 2012 at 19:39 Comment(1)
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.Tenter
T
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.

Thigpen answered 5/11, 2008 at 10:11 Comment(3)
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.Viewpoint
"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...Laurelaureano
and those are normally what you're actually interested in.Laurelaureano

© 2022 - 2024 — McMap. All rights reserved.