我按课程代码写的异常没被捕获,什么情况? package handler; import com.imooc.domain.Result; import com.imooc.utils.ResultUtil; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * Created by MBENBEN on 2017/6/18. */ @ControllerAdvice public class ExceptionHandle { @ExceptionHandler(value = Exception.class) @ResponseBody public Result handle(Exception e){ return ResultUtil.error(100,e.getMessage()); } } service里 public void getAge(Integer id) throws Exception{ Girl girl= girlRepository.findOne(id); Integer age=girl.getAge(); if(age<10){ throw="" new="" else="" age="">10 && age<16){ //你可能还在上初中 throw new Exception("你可能还在上初中"); } //如果大于16岁 } controller里 @GetMapping(value = "girls/getAge/{id}") public void getAge(@PathVariable("id") Integer id) throws Exception{ girlService.getAge(id); }
感谢感谢哈哈哈哈
谢谢老铁
感谢了,老铁
后来发现问题了,代码都是正确的,注意handle包必须和controller包和service包在同一级包下才行!!