項目使用了反射進行方法調用,被調用的方法內拋出自己封裝的Exception,外部無法捕捉到自定義Exception,捕捉到InvocationTargetException。
java.lang.ClassCastException: java.lang.reflect.InvocationTargetException
cannot be cast to com.test.test.exception.BaseException
public <T> BaseResponse handleRequest(T param, Integer type) { BaseResponse br = null; WHMethod methodEnum = WHMethod.getByMethod(type); String responseMsg = null; String responseCode = null; if (methodEnum == null) { logger.error("未獲取到需要執行的方法"); throw new BaseException(BackCode.GET_METHOD_NULL_ERROR); } try { String method = methodEnum.getMethod(); Method m = this.getClass().getMethod(method, new Class[] { methodEnum.getRequestType() }); br = (BaseResponse) m.invoke(whHouseStrategy, param); } catch (Exception e) { Throwable throwable = e.getCause(); if (throwable instanceof BaseException) { responseMsg = ((BaseException) throwable).getDescription(); responseCode = ((BaseException) throwable).getErrorcode(); throw new BaseException(BackCode.NET_ERROR.getIndex(), responseMsg); } logger.error("【handleRequest公共請求方法】調用接口錯誤,詳細信息:" + ExceptionUtils.getStackTrace(e)); throw new BaseException(BackCode.NET_ERROR.getIndex(), methodEnum.getErrorMsg()); } return br; }