[英]Overriding method throws checked exception not defined in interface
說我有一個接口DataStore
,它具有方法public List<Entity> getEntities()
。 SqlDataStore
類實現此接口。 問題是,當調用SqlDataStore.getEntities()
,可能會引發SQLException
。 我無法更改將此檢查的異常添加到方法簽名的接口,因為您還可以編寫實現XmlDataStore
來訪問XML文件以獲取數據。 我應該如何處理實現可能引發的那些檢查過的異常?
好的,這是我缺乏更好的主意時所做的事情。 我定義了一個擴展了RuntimeException
DataStoreException
類。 我從接口定義中刪除了throws子句。 當在實現DataStore
的類之一中引發檢查異常時,我將其捕獲並將其包裝在DataStoreException
並重新拋出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.