Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out what kind of exceptions can be thrown by a method?

I'm writing code that calls the following method that exists in Windows.Networking.PushNotifications

// Summary:
//     Creates objects that you use to retrieve push notification channels from
//     the Windows Push Notification Services (WNS). These channels are bound to
//     an app or secondary tile.
[Threading(ThreadingModel.MTA)]
[Version(100794368)]
public static class PushNotificationChannelManager
{
    // Summary:
    //     Creates an object, bound to the calling app, through which you retrieve a
    //     push notification channel from Windows Push Notification Services (WNS).
    //
    // Returns:
    //     The object, bound to the calling app, that is used to request a PushNotificationChannel
    //     from the Windows Push Notification Services (WNS).
    [Overload("CreatePushNotificationChannelForApplicationAsync")]
    public static IAsyncOperation<PushNotificationChannel> CreatePushNotificationChannelForApplicationAsync();

}

I want to be sure to cover all cases where exceptions can be thrown and deal with each appropriately. Is it possible for me to get a list of the different types of exceptions this can throw and different circumstances which could cause them?

I don't want to just have a catch-all

catch(Exception ex) { }    

This MSDN article states that "An exception is thrown if you attempt to register a WNS push notification channel when there is no data connection". However it doesn't state what type of exception or if there are other cases when an exception can be thrown.

How can I find out if there are other possible exception types?

like image 461
DaveDev Avatar asked Nov 04 '14 14:11

DaveDev


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.

How many exceptions can be thrown by a method?

As seen in the syntax above, all exceptions that can be thrown by a method should be declared in the method signature using the throws keyword. A method can throw multiple exceptions, which should be separated by a comma in the declaration.

Which exceptions can be thrown?

Before you can catch an exception, some code somewhere must throw one. 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.

How do you know if a method throws an exception?

The calculate method should check for an exception and if there is no exception, return the calculated value to the main function i.e. v1+v2 or v1-v2; Else if an exception exists then it should print the error statement and the value that is returned from the calculate method to the main method should be 0.0(Not ...


2 Answers

The only way is documentation and source code, although notice that so many other exceptions might be thrown like OutOfMemoryException, TypeLoadException, ThreadAbortException and ... so the right approach in my opinion is catch the exceptions you can do something about them and let others bubble up the call stack.

also read this Vexing exceptions

like image 116
Hamid Pourjam Avatar answered Sep 24 '22 11:09

Hamid Pourjam


If you can simulate one of the error conditions mentioned in that link, you should be able to use a generic exception catch to see the type that's thrown.

I think it's going to throw a System.Runtime.InteropServices.COMException though based on the error codes mentioned in the article though.

like image 31
Brian Dishaw Avatar answered Sep 22 '22 11:09

Brian Dishaw