spring - 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?
解决方案
兄弟,我们在上面使用@ExceptionHandler 一个方法,我们希望处理代码中发生的所有异常,而在尝试捕获的情况下,您只需处理仅发生的异常。但是,如果我们希望程序中发生的每个异常都由一种方法处理,以使其看起来干净而专业,我们使用@ExceptionHandler。这是可以帮助您理解这一点的链接。 https://howtodoinjava.com/spring-core/spring-exceptionhandler-annotation/
让我知道这是否有帮助。
推荐阅读
- google-cloud-platform - 如何将 Google 表格数据加载到 Google Cloud MySQL?
- reactjs - 使用 sinon 存根测试辅助函数
- grails - 如何在 Grails 3 中启用 Tomcat 访问日志?
- python - 使用 LSTM 层在 Keras 中进行预测
- django - 突出显示未翻译的 Django 字符串
- r - 将 R Markdown 渲染到 MS Word 时页面上的弹性表的对齐方式
- python - 似乎令人满意时,INSERT 上的 MySQL Python 查询语法错误?
- google-sheets - 在 Google Data Studio 中合并列数据
- asp.net - 哪个 ValueProviderFactory 采用表单数据?
- c++11 - C++ 模板:类型参数以检查验证