首页 > 解决方案 > Difference between try catch and @ExceptionHandler in Spring REST

问题描述

I was going through a video tutorial where the instructor focussed on handling any Runtime exceptions through the @ExceptionHandler in Spring.

What is the difference between the approach of handling it via try catch or via @ExceptionHandler?Which is the better approach and when to use either of them?

标签: springspring-data-rest

解决方案


兄弟,我们在上面使用@ExceptionHandler 一个方法,我们希望处理代码中发生的所有异常,而在尝试捕获的情况下,您只需处理仅发生的异常。但是,如果我们希望程序中发生的每个异常都由一种方法处理,以使其看起来干净而专业,我们使用@ExceptionHandler。这是可以帮助您理解这一点的链接。 https://howtodoinjava.com/spring-core/spring-exceptionhandler-annotation/

让我知道这是否有帮助。


推荐阅读