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

时间:2014-11-04 14:47:34

标签: c# .net exception exception-handling

我正在编写调用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();

}

我想确保涵盖所有可以抛出异常并适当处理每种情况的情况。我是否有可能获得可能引发的不同类型的异常列表以及可能导致它们的不同情况?

我不想拥有一个全能的

catch(Exception ex) { }    

This MSDN article表示&#34;如果您在没有数据连接时尝试注册WNS推送通知通道,则会引发异常&#34;。但是它没有说明什么类型的异常,或者是否存在可以抛出异常的其他情况。

如何确定是否存在其他可能的例外类型?

3 个答案:

答案 0 :(得分:4)

唯一的方法是文档和源代码,虽然注意到可能会抛出许多其他异常,例如OutOfMemoryExceptionTypeLoadExceptionThreadAbortException和...所以正确的方法在我的意见是捕捉你可以对它们做些什么的例外,并让其他人冒泡调用堆栈。

还请阅读此Vexing exceptions

答案 1 :(得分:1)

如果您可以模拟该链接中提到的错误条件之一,则应该能够使用通用异常catch来查看引发的类型。

我认为它会根据文章中提到的错误代码抛出System.Runtime.InteropServices.COMException

答案 2 :(得分:1)

  

我想确保涵盖所有可以抛出异常并适当处理的情况

不可能涵盖所有可以抛出异常并适当处理的情况。

如果您完全了解可以抛出特定异常的情况,并了解程序的不变量,并知道它们仍然有效,或者如何恢复它们,那么您可以从恢复例外。

但是,例如,您可能遇到由宇宙射线引起的异常,该异常会破坏机器的状态,从而导致访问冲突。你怎么会从中恢复过来的?总而言之,您可以到达程序必须向其提出的方式并向支持人员进行分页。

相关问题