java - 是否可以在 Java 中创建我自己的未经检查的异常?

标签 java error-handling

是否可以在 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/

相关文章:

apache - 将 PHP 设置为始终在错误 500 时重定向

vba - MS Access VBA : how to trap case where delete hangs due to low memory or disk space?

vbscript - 如何退出函数中的每个循环?

error-handling - Selenium IDE自定义错误消息

java - String 到 Byte[] 和 Byte 到 String

java - 如何将项目导入Android Studio

java - Spring MVC 和 PDF

Python:图的断开组件中的所有最短路径

java - 需要一种可中断的方式来监听工作线程中的 UDP 数据包

java - 我可以在 server.xml 的哪个位置添加 -Djavax.net.debug=all 是在 debugArgs 的 jvmEntries 中吗?