我在 DAO 方法中有以下代码:
public void someMethod() throws CustomException {
try {
... do something ...
}catch(Exception e) {
if(e.getCause() instanceOf org.hibernate.ConstraintViolationException && e.getMessage().contains("child record found")) {
throw new CustomException("Child records found.");
}else {
throw new CustomException("Unable to update.");
}
}
}
在服务层:
public void someMethod() throws CustomException {
dao.someMethod();
}
在控制器中:
public ResponseObject someMethod() {
ResponseObject response = new ResponseObject();
try {
service.someMethod();
response.setMessage("success");
}catch(CustomException e) {
response.setMessage(e.getMessage());
}
return response;
}
我是否按照最佳实践正确进行了操作?还有什么可以做来使它更加合适?
非常感谢您的帮助!