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.
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.
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.
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).
.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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With