关于业务层的自定义异常,不能在全局拦截器捕捉的问题

@JFinal 波总,我在业务层主动抛出了一个异常,但却不能在全局拦截器被捕捉,请帮忙分析一下。

1. Db.query("select from ccc");// 用一个SQL语法错误触发可以在全局拦截器捕捉;
2. throw new MySQLSyntaxErrorException("skuid对应的商家商品不存在");//手动抛出不能被全局拦截器捕捉。

业务方法:

Float price = Db.queryFloat("select retailPrice from store_stock where id=?",orderDetail.getSkuId());

if (price==null){

map.put("result", "-1");

map.put("msg", "skuid对应的商家商品不存在");

throw new SQLException("skuid对应的商家商品不存在");

-------------------------------

全局拦截器相关:

public class ExceptionInterceptor implements Interceptor{

@Override

public void intercept(Invocation inv) {

// TODO Auto-generated method stub

Controller c = inv.getController();

// HttpServletRequest request = c.getRequest();

try{

inv.invoke();

}catch (ActionException e){

throw e;

}

catch(Exception e){

//System.out.println(e.getMessage());

e.printStackTrace();

c.renderJson("{\"result\":-255,\"msg\":"+e.getMessage()+"}");

}

}

}



评论区

JFinal

2016-09-17 11:21

这种问题极度容易解决,照这个思脉处理一下:
1:查看控制台的 JFinal Action Report 输出,看ExceptionInterceptor是否拦截了该请求
2:确保当前请求拦截以后,在 catch 块中设置断点,看是否捕捉到异常
3:如果没有捕捉到,再检查一下是否有别的拦截器将异常吃掉没有再次抛出
4:注意改过的代码是否不生效,eclipse 有时会有这个 bug
5:终极方案,单步调试,看程序到底怎么走的。建议直接使用终极方案,少走弯路

chenwang

2016-09-17 11:40

@JFinal
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.jfinal.aop.Invocation.invoke(Invocation.java:87)
at com.jfinal.plugin.activerecord.tx.Tx.intercept(Tx.java:77)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at interceptor.ExceptionInterceptor.intercept(ExceptionInterceptor.java:17)
at com.jfinal.aop.Invocation.invoke(Invocation.java:67)
at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74)
at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:365)
at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.jfinal.aop.Invocation.invoke(Invocation.java:73)
... 29 more
Caused by: java.sql.SQLException: skuid对应的商家商品不存在
at service.OrderService.orderCreate(OrderService.java:47)
at controller.OrderController.orderCreate(OrderController.java:21)
... 34 more
上面是异常stack,捕捉到的是中间那个异常java.lang.reflect.InvocationTargetException,我想获得java.sql.SQLException: skuid对应的商家商品不存在

chenwang

2016-09-17 11:55

@JFinal 波总,我知道了,是捕捉了最后一个,我想办法得到第一个就可以。谢谢。

热门反馈

扫码入社