处理层之间的异常

时间:2018-09-11 16:51:57

标签: exception-handling ejb oracle-adf throw n-tier-architecture

我目前有无法解决的问题。我在JDeveloper中有一个使用ADF的项目。 当模型层中某处发生异常(Throwable)时,我无法在视图层中捕获到它。 让我更好地解释一下:

在视图中,我有Transaction.java其中

public void save(){
     try{ 
         Boolean res= (Boolean) context.get("res"); //value obtained from front-end
         OperationBinding op = this.getBindings().getOperationBinding("methodThatThrowsException"); 
         op.getParamsMap().put("res", res);
         op.execute();//line that should throw exception
     }catch(ClassCastException cce){
          showMessage(cce.getMessage()); //pop up with a description of the exception
}

然后,在模型层:

public void methodThatThrowsException(Boolean res) throws ClassCastException {
     try{ 
         Object obj = res;
         Integer badCoding = (Integer) obj; //line that throws exception
     }catch(ClassCastException e){
           //clean-up code
        }
}

methodThatThrowsException将引发ClassCastException,但不会被save方法捕获。 我不知道怎么了我什至尝试在save之后添加op.execute();

并失败了
if(!op.getErrors().isEmpty()){
for(Throwable t : op.getErrors()){
    if (t instanceof Exception){
        Exception e = (Exception) t;
        if (e instanceof RuntimeException)
            RuntimeException re = (RuntimeException) e;
            if(re instanceof ClassCastException)
                throw t;
    }

}

但是采用这种可怕的解决方法,永远不会抛出t,因为它在View层上是JBOException,而不是ClassCastException

我该怎么办?任何建议现在都对我有用。预先感谢。

1 个答案:

答案 0 :(得分:0)

对于熟悉Java的任何人来说,绑定层的异常处理都是违反直觉的:

Boolean res= (Boolean) context.get("res"); //value obtained from front-end
perationBinding op = this.getBindings().getOperationBinding("methodThatThrowsException"); 
op.getParamsMap().put("res", res);
op.execute();//line that should throw exception
//grab the exception this way
op.getErrors()