java 中断程序_java有什么方法可以捕获程序异常的?然后中断程序,发送信息给前端...

试试这个?

@ControllerAdvice

public class GlobalExceptionHandlingControllerAdvice {

protected Logger logger;

public GlobalExceptionHandlingControllerAdvice() {

logger = LoggerFactory.getLogger(getClass());

}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/* . . . . . . . . . . . . . EXCEPTION HANDLERS . . . . . . . . . . . . . . */

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

/**

* Convert a predefined exception to an HTTP Status code

*/

@ResponseStatus(value = HttpStatus.CONFLICT, reason = "Data integrity violation")

// 409

@ExceptionHandler(DataIntegrityViolationException.class)

public void conflict() {

logger.error("Request raised a DataIntegrityViolationException");

// Nothing to do

}

/**

* Convert a predefined exception to an HTTP Status code and specify the

* name of a specific view that will be used to display the error.

*

* @return Exception view.

*/

@ExceptionHandler({ SQLException.class, DataAccessException.class })

public String databaseError(Exception exception) {

// Nothing to do. Return value 'databaseError' used as logical view name

// of an error page, passed to view-resolver(s) in usual way.

logger.error("Request raised " + exception.getClass().getSimpleName());

return "databaseError";

}

/**

* Demonstrates how to take total control - setup a model, add useful

* information and return the "support" view name. This method explicitly

* creates and returns

*

* @param req

* Current HTTP request.

* @param exception

* The exception thrown - always {@link SupportInfoException}.

* @return The model and view used by the DispatcherServlet to generate

* output.

* @throws Exception

*/

@ExceptionHandler(SupportInfoException.class)

public ModelAndView handleError(HttpServletRequest req, Exception exception)

throws Exception {

// Rethrow annotated exceptions or they will be processed here instead.

if (AnnotationUtils.findAnnotation(exception.getClass(),

ResponseStatus.class) != null)

throw exception;

logger.error("Request: " + req.getRequestURI() + " raised " + exception);

ModelAndView mav = new ModelAndView();

mav.addObject("exception", exception);

mav.addObject("url", req.getRequestURL());

mav.addObject("timestamp", new Date().toString());

mav.addObject("status", 500);

mav.setViewName("support");

return mav;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值