我有如下实现的方法类:我们可以重构这些方法吗?
void methodOne() {
try {
getHelper().doActionOne();
} catch (Exception ex) {
throw new CustomException(ex);
}
}
void methodTwo() {
try {
getHelper().doActionTwo();
} catch (Exception ex) {
throw new CustomException(ex);
}
}
void methodThree() {
try {
getHelper().doActionThree();
} catch (Exception ex) {
throw new CustomException(ex);
}
}
void methodFour;
void methodFive;
...
有没有更好的方式来做到这一点?这些代码让我感到不舒服。
编辑: 对不起,不清楚的例子。我执行与Hibernate GenericDao类,真正的代码是这样的:
class GenericDaoImpl<T, PK> {
PK create(T object) {
try {
getSession().save(object);
} catch(Exception ex) {
throw new DataAccessLayerException(ex);// wrap any exception to my exception
}
}
T read(PK id) {
try {
getSession().get(T.class, id);
} catch (Exception ex) {
throw new DataAccessLayerException(ex);
}
}
void update(T object);
void delete(T object);
}
能否请您解释一下在这些方法被称为上下文? – Ammu 2011-04-19 03:16:38
为什么代码会让你感到不舒服?我没有看到你的代码有什么特别的错误。 – 2011-04-19 03:22:08
目前还不清楚你想要完成什么,所以很难推荐更好的方法。你为什么捕获所有异常并用自定义异常包装它们?它看起来像你试图避免所有检查异常与未经检查的异常。 – WhiteFang34 2011-04-19 03:22:28