zoukankan      html  css  js  c++  java
  • 反射抛出异常,如何确定异常类型

    参考代码:

    private String invokeMethod(ForeignService foreignService, Map<String, Object> paramMap) throws Exception {
    Object resultObj = null;
    try {
    Object bean = SpringUtil.getBean(foreignService.getHandlerService());
    Class beanClass = bean.getClass();
    Method method = beanClass.getMethod(foreignService.getHandlerMethod(),ForeignService.class, Map.class);
    if (Assert.checkParam(paramMap)) {
    resultObj = method.invoke(bean, foreignService,paramMap);
    }
    } catch (Exception e) {
    LOG.error("调用外部接口反射异常,foreignServiceId={}", foreignService.getId());
    throw e;
    }
    return (String)resultObj;
    }

    外部调用以上方法时, try catch 需要这样写:

    } catch (Exception ex) {
    ex.printStackTrace();
    resultLevel = RuleLevelEnum.ERROR.getStatus();
    LOG.error("xxx:{}", ex.getMessage());
    resultDetailMap.put("errMsg", "xxx");
    if(ex instanceof InvocationTargetException) {
    InvocationTargetException targetEx = (InvocationTargetException)ex;
    Throwable t = targetEx .getTargetException();
    if(t instanceof BussinessException) {
    // xxx
    resultLevel = RuleLevelEnum.WARNING.getStatus();
    BussinessException bussinessEx = (BussinessException)t;
    if (bussinessEx.getErrCode() == BussinessExceptionEnum.INTERFACE_TIMEOUT.getCode()) {
    interfaceExceptionFlag = Boolean.TRUE;
    resultDetailMap.put("errMsg", "xxx");
    }
    if (bussinessEx.getErrCode() == BussinessExceptionEnum.INTERFACE_EXCEPTION.getCode()) {
    interfaceExceptionFlag = Boolean.TRUE;
    resultDetailMap.put("errMsg", "xxx");
    }
    }
    }
    }

  • 相关阅读:
    【BZOJ】1486 [HNOI2009]最小圈
    【网络流24题】
    【网络流24题】魔术球问题
    【网络流24题】最小路径覆盖问题
    【BZOJ】1026 [SCOI2009]windy数
    【SPOJ】2319 BIGSEQ
    【SPOJ】1182 Sorted bit sequence
    虔诚的墓主人(bzoj 1227)
    Round Numbers(poj 3252)
    windy数(bzoj 1227)
  • 原文地址:https://www.cnblogs.com/maohuidong/p/10728385.html
Copyright © 2011-2022 走看看