遇到问题
- 自定义异样:LogicExcetion
- 在办法调用的最外层method.invoke()处catch(LoginException e)
- 办法抛LogicExcetion未被捕捉,控制台输入InvocationTargetException
剖析起因
如图:
反射办法只返回以上几种异样,其中InvocationTargetException的形容:如果底层办法抛出一个异样,则会封账在该异样中进行抛出
解决问题
InvocationTargetException异样中的变量target即为底层办法抛出的异样,所以能够这样批改,贴源码:
try { Object res = method.invoke(handler, params); if (res instanceof Message) { Message message = (Message) res; session.sendPacket(message); } } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); if (cause instanceof LogicException) { LogicException le = (LogicException) cause; switch (le.getType()) { case CLIENT_EXCEPTION: session.sendPacket(new ResLogicError(le.getMsg())); break; case LOGIC_EXCEPTION: logger.error("代码逻辑谬误:{}", le.getMsg()); break; default: break; } } }
最初
集体简略了解,不对之处还望斧正!