简体   繁体   English

如何找出方法可以抛出哪种异常?

[英]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 我正在编写调用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". 此MSDN文章指出“如果您在没有数据连接时尝试注册WNS推送通知通道,则会引发异常”。 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. 唯一的方法是文档和源代码,虽然注意到可能会抛出许多其他异常,如OutOfMemoryExceptionTypeLoadExceptionThreadAbortException和...所以我认为正确的方法是捕获例外,你可以对它们做些什么,让其他人冒泡调用堆栈。

also read this Vexing exceptions 还阅读了这个Vexing异常

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. 如果您可以模拟该链接中提到的错误条件之一,那么您应该能够使用通用异常catch来查看抛出的类型。

I think it's going to throw a System.Runtime.InteropServices.COMException though based on the error codes mentioned in the article though. 我认为它会抛出一个System.Runtime.InteropServices.COMException但是基于文章中提到的错误代码。

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. 总而言之,您可以到达程序必须向其提供的位置并为支持人员提供页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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