我期待創造一個例外工廠Java異常工廠較高水平異常
我有一個連接到不同的數據存儲和這些數據存儲拋出一些例外的服務。 我不想將這些例外返回給服務的用戶,而是返回更高級別的例外 storeServiceTransientReadException
當連接到數據存儲時,我將使用try,catch拋出模式;例如,對於卡桑德拉連接:
public ResultSet safeExecute(Statement statement) throws StoreServiceException {
try {
return session.execute(statement);
}
catch(QueryExecutionException ex){
log.error(ex);
StoreServiceException storeException = StoreServiceExceptionFactory.getTransientException(ex);
throw storeException;
}
在卡桑德拉的例子,我想該工廠創建一個讀或寫這取決於是否異常是ReadFailureException,ReadTimeoutException,WriteFailureException或WriteTimeoutException
對於其他數據storeServiceException例外商店我想要遵循相同的模式,那麼服務的用戶只需要擔心服務錯誤而不是特定的數據存儲錯誤。
對於工廠我想沿(僞)線的東西:
public class ExceptionsFactory {
public StoreServiceTransientException getTransientException(Exception ex){
if ReadException
return StoreServiceTransientException("read exception ")
if WriteException
return StoreServiceTransientException("write exception ")
}
public StoreServiceNonTransientException getTransientNonException(Exception ex){
if ReadException
return StoreServiceNonTransientException("read exception ")
if WriteException
return StoreServiceNonTransientException("write exception ")
}
但我無法找到這裏面我擔心許多網上的例子。 這是一個非常糟糕的主意? 我應該只有更多特定的catch塊返回我想要的storeServiceException?
在Java中,通常的做法是類名以大寫字母開頭。 'StoreServiceTransientException'將是一個快速但巨大的改進 – byxor
@byxor好點 - 當我嘗試使示例更通用 – JJHolloway