【问题标题】:Handling exceptions between tiers处理层之间的异常
【发布时间】:2019-02-16 05:48:56
【问题描述】:

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

在视图中,我有 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 方法捕获。 我不知道怎么了。我什至尝试在op.execute(); 之后添加save,但失败了

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 永远不会被抛出,因为它是 JBOException,而不是在 View 层上时的 ClassCastException

我能做什么?任何建议现在都对我有用。提前致谢。

【问题讨论】:

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


    【解决方案1】:

    对于熟悉 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()
    

    【讨论】:

    • 我试过这种方法。我只能知道是否有错误,但我无法抛出该异常,也不知道是哪种类型,因此我不知道发生错误后该怎么办
    • op.getErrors() 返回一个大小为 1 的 Exception 对象的集合。当然你可以知道是哪种类型 - 使用 instanceof 来确定类型。
    • 这实际上是问题所在。我不知道类型,因为它是 JBOException,而不是我在测试代码中生成的 ClassCastException。请参阅原帖的最后部分。
    • 检查 JboException 的根本原因,你会得到你的 ClassCastException。
    猜你喜欢
    • 1970-01-01
    • 2013-10-15
    • 2015-12-15
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多