在ExceptionHandle模块下,(e instanceof GirlException)判断为真了,执行语句还要进行强制转换吗?

来源:2-6 统一异常处理(下)

麻了一一一

2018-12-10 22:22

public class ExceptionHandle {

    //这里是捕获异常并处理的方法
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof GirlException) {
            //这里的强制类型转换是必须的吗?如果是,上面的条件判断在判断什么?
            GirlException girlException = (GirlException) e;
            return ResultUtil.failure(girlException.getCode(), girlException.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            return ResultUtil.failure(-1, "未知错误");
        }
    }
}


写回答 关注

1回答

  • littlematt
    2019-05-16 11:20:18

    要强转,要不然怎么获取相应的code和message

Spring Boot进阶之Web进阶

《2小时学习Spring Boot》之进阶教程,针对Web方面的相关技巧

104255 学习 · 425 问题

查看课程

相似问题