[英]How can I find out what kind of exceptions can be thrown by a method?
我正在編寫調用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) { }
此MSDN文章指出“如果您在沒有數據連接時嘗試注冊WNS推送通知通道,則會引發異常”。 但是,它沒有說明什么類型的異常,或者是否存在可以拋出異常的其他情況。
如何確定是否存在其他可能的異常類型?
唯一的方法是文檔和源代碼,雖然注意到可能會拋出許多其他異常,如OutOfMemoryException
, TypeLoadException
, ThreadAbortException
和...所以我認為正確的方法是捕獲例外,你可以對它們做些什么,讓其他人冒泡調用堆棧。
還閱讀了這個Vexing異常
如果您可以模擬該鏈接中提到的錯誤條件之一,那么您應該能夠使用通用異常catch來查看拋出的類型。
我認為它會拋出一個System.Runtime.InteropServices.COMException
但是基於文章中提到的錯誤代碼。
我想確保涵蓋所有可以拋出異常的情況並適當地處理每個情況
不可能涵蓋所有可以拋出異常並適當處理每種情況的情況。
如果您完全理解可以拋出特定異常的情況,並且您了解程序的不變量,並且知道它們仍然有效,或者如何恢復它們,那么您可以從該異常中恢復。
但是,例如,您可能會遇到由宇宙射線引起的異常,該異常會破壞機器的狀態,從而導致訪問沖突。 你怎么會從中恢復過來的? 總而言之,您可以到達程序必須向其提供的位置並為支持人員提供頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.