Exception handling 在Camel中处理包装异常
现在,我的“someBeanID”在处理时抛出ChildException,但正在为此调用ParentExceptionHandler。“someBeanID”中的代码片段如下Exception handling 在Camel中处理包装异常,exception-handling,apache-camel,Exception Handling,Apache Camel,现在,我的“someBeanID”在处理时抛出ChildException,但正在为此调用ParentExceptionHandler。“someBeanID”中的代码片段如下 class MyRouteBuilder extends SpringRouteBuilder { public void configure() throws Exception { //initialize camel context here onException(Chi
class MyRouteBuilder extends SpringRouteBuilder {
public void configure() throws Exception {
//initialize camel context here
onException(ChildException.class)
.process(new ChildExceptionHandler())
.handled(true)
.to(errorURI);
onException(ParentException.class)
.process(new ParentExceptionHandler())
.handled(true)
.to(errorURI);
from(startURI)
.processRef("someBeanID")
//other processing here
}
}
似乎每当我们包装任何异常时,Camel都会自动找到实际包装的异常并为此调用处理程序,而不是为包装异常调用处理程序。为什么会这样?我的代码有问题吗
谢谢,终于解决了……请参阅 捕获多个异常时,OneException子句的顺序非常重要。ApacheCamel最初尝试将抛出的异常与第一个子句相匹配。如果第一个子句不匹配,将尝试下一个OneException子句,依此类推,直到找到匹配项。每个匹配尝试都由以下算法控制: 如果抛出的异常是链式异常(即,捕获异常并将其作为不同的异常重新抛出),则最嵌套的异常类型最初用作匹配的基础。此异常测试如下所示: 如果要测试的异常具有OneException子句中指定的类型(使用instanceof测试),则会触发匹配 如果要测试的异常是OneException子句中指定类型的子类型,则会触发匹配 如果嵌套最多的异常未能生成匹配项,则将测试链中的下一个异常(包装异常)。测试在链的上游继续进行,直到触发匹配或链耗尽
try {
//some processing
throws new ParentException();
} catch (ParentException e) {
throw new ChildException(e); //being handled by ParentExceptionHandler (why?? should be ChildExceptionHandler??)
throw new ChildException(); //being handled by ChildExceptionHandler (should be)
}