我创建了一个自定义异常来处理我的业务场景需要在验证失败时将此异常推回给调用者
路由器呼叫定义
from(jettyEndpoint_incSubsCnt)
.doTry().bean(MyProcessor.class).doCatch(MyException.class).bean(MyThrowableException.class);
我的Junit实施
try{template.sendBody(new Gson().toJson(myrequest,
MyRequest.class));
}catch(Exception e){
e.printStackTrace();
}
我在MyProcessor中抛出了一个customException
if(errors.hasErrors()){
throw new MyException(ChangeSub_routerClli,errors.getAllErrors().get(0).getDefaultMessage());
}
我的自定义异常类
public class MyException extends Exception{
private int exceptionID;
public MyException(int exceptionID) {
super();
this.exceptionID = exceptionID;
}
public MyException() {
}
public MyException(int exceptionID,String message) {
super(message);
this.exceptionID = exceptionID;
}
public MyException(int exceptionID,String message,Throwable exception){
super(message,exception);
this.exceptionID = exceptionID;
}
@Override
public String toString() {
return super.toString();
}
@Override
public String getMessage() {
return super.getMessage() + " for Exception ID :" + exceptionID;
}
public int getExceptionID() {
return exceptionID;
}}
我的throwable类实现
public class MyThrowableException implements Processor{
Logger log = Logger.getLogger(MyThrowableException.class);
@Override
public void process(Exchange exchange) throws Exception {
// TODO Auto-generated method stub
Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);
log.error(exception);
throw exception;
}}
我的日志
Logs :::[qtp2078517710-16 - myURL?exchangePattern=InOut] ERROR MyException - MyException: routerClli is required. for Exception ID :200
我无法将异常退回给调用者
答案 0 :(得分:0)
两件事:
.end()
.handled(false)
查看try-catch上的文档。