How to tell whitch exceptions a .NET call throws?

If one has the following code:

data.SaveChanges();

(data is an ObjectContext)

The MSDN doc has listed the OptimisticConcurrencyException as thrown. That's fine but I known that a UpdateException can also be thrown (and possibly others too). How can I know which exceptions a method can throw?

I do not want to catch Exception as I only want to catch exceptions which I know I can handle in some way. This is generally speaking - not just for the example above. There must be some way of knowing which exception a 'built-in' .NET method is throwing.

Upvotes: 1

Views: 197

Answers (2)

amdmax
amdmax

Reputation: 761

Just in case (sorry if it's obvious but there're guys who don't know it) you can hover your mouse over the class name / method call in your editor view in VS. It shows you all the exceptions that can be thrown by the method if defined in documentation.

Upvotes: 0

SLaks
SLaks

Reputation: 887305

That's not a "native" method; it's an ordinary method that happens to be written by Microsoft rather than you.
Actual native methods cannot throw managed exceptions (although COM interop will convert things to managed exceptions)

Unlike Java, C# does not have exception specifications, so there is no inherent way of knowing what exceptions a method will throw.

Your only options are the documentation or a decompiler.

Upvotes: 5

Related Questions