我期待创造一个例外工厂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