是否可以在 Java 中创建我自己的未经检查的异常?
基本上,我愿意
- 有我自己的异常类,
MyUncheckedException
- 这不会让我用大量自己的
throws
更新所有调用引发此异常的方法的方法
最佳答案
是的。从 RuntimeException
扩展 MyUncheckedException
.但是,以下是来自 documentation 的指南
Generally speaking, do not throw a RuntimeException or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw.
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
关于java - 是否可以在 Java 中创建我自己的未经检查的异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9371710/