简体   繁体   中英

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?

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

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.

I want to be sure to cover all cases where exceptions can be thrown and deal with each appropriately

It is not possible to cover all cases where exceptions can be thrown and deal with each appropriately.

If you fully understand the circumstances where a particular exception can be thrown, and you understand the invariants of the program, and know that either they are still valid, or how to restore them, then you can recover from that exception.

But you may have, for example, an exception caused by a cosmic ray which corrupts the state of the machine, resulting in an access violation. How are you going to recover from that? There is always a point you get to where the program has to throw up its hands and page the support staff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM