Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSDN library - how to know what exceptions a method can throw?

I'm from an Android background and when looking up a method in the Android developers reference, the information usually includes what exceptions the method can throw (as well as the parameters the method takes in and its return type). I've had a browse of some classes in the MSDN library and this doesn't seem to be the case here. So how, when developing, can I determine what exceptions a method can throw (if it throws any exceptions)?

A concrete example is the DataContext.SubmitChanges() method (MSDN link), which can throw an SqlCeException exception. It seems there's no way of picking up on this unless it's encountered accidentally at run-time.

like image 272
Adil Hussain Avatar asked Jun 05 '12 20:06

Adil Hussain


People also ask

How do you decide if an exception should be thrown in a method?

An exception should be thrown when a function experiences a failure, i.e., an error. A function is a unit of work, and failures should be viewed as errors or otherwise based on their impact on functions.

Which exceptions can be thrown?

Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement.

How many exceptions can a method throw?

A method can throw one of several exceptions. Eg: public void dosomething() throws IOException, AWTException { // .... } This signals that the method can eventually throw one of those two exceptions (and also any of the unchecked exceptions).


2 Answers

.NET is a bit different than java in exceptions. There is no throws syntax, where you have to declare what types of exceptions can be thrown from the method. Every method can possibly throw any kind of exception. That's why not always MSDN documentation contains that kind of data.

When you can't find list of possible exceptions on MSDN pages you can search/ask about it on sites like stackoverflow (eg. for DataContext.SubmitChanges()) or just test your app and try to generate the exception to check what type it is.

like image 164
MarcinJuraszek Avatar answered Oct 28 '22 12:10

MarcinJuraszek


There is no equivalent to the throws keyword in .net, but you can tell your user what exceptions you know your method may throw in your doc-comments (C# equivalent to java doc)

like image 22
BenCamps Avatar answered Oct 28 '22 13:10

BenCamps