[英]In a Spring Boot application, how would I catch internally thrown exceptions that would otherwise escape to the client, but not Spring MVC exceptions?
[英]Why do I NOT need to catch certain exceptions in a Spring Boot MVC application?
我有一个简单的 Spring Boot MVC 应用程序,我想在其中从服务层抛出异常。 我使用一系列@ExceptionHandler 注释方法在控制器中处理这些异常。
我不明白为什么有些异常不需要声明为抛出,而有些则需要。 例如,如果我只是抛出一个
java.sql.SQLIntegrityConstraintViolationException
编译器抱怨:
java: unreported exception java.sql.SQLIntegrityConstraintViolationException; must be caught or declared to be thrown
而如果我抛出一个
org.springframework.dao.DataIntegrityViolationException
我既不需要声明它被抛出,也不需要在控制器中捕获它。 它只是由我的 ExceptionHandler 处理。
有什么不同? 更好的是,我在哪里可以找到这方面的文档?
这与Spring无关,它是Java的核心。 Java 有受检异常和非受检异常。 Checked 需要被check,这些异常是在编译时检查的。 如果某个方法中的某些代码抛出了已检查的异常,则该方法必须要么处理该异常,要么必须使用 throws 关键字指定该异常。 另一方面,您有未经检查的异常 - Error 或 RuntimeException 子类 - 对于这些异常,您不必声明throws
我相信这个问题与许多其他问题重复:) 检查此已检查与未检查异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.