java - 为什么 IllegalArgumentException (JDK 1.4.2) 不能用可抛出的原因构造?

标签 java exception

来自扩展 java.beans.PropertyEditorSupport 的类:

/**
 * Sets the property value by parsing a given String.  May raise
 * java.lang.IllegalArgumentException if either the String is
 * badly formatted or if this kind of property can't be expressed
 * as text.
 *
 * @param text  The string to be parsed.
 */
public void setAsText(String name) {
    try {
        asEnum(name);
    } catch (InvalidEnumNameException e) {
        throw new IllegalArgumentException("Unable to convert value: "+ name);
    }
}

将导致真实的堆栈跟踪丢失。

最佳答案

IllegalArgumentException does have constructors带有 Throwable cause 参数 - 该代码根本不使用它们,可能是因为它比“异常有一个 Throwable cause”约定,该约定是随 Java 5 引入的。

关于java - 为什么 IllegalArgumentException (JDK 1.4.2) 不能用可抛出的原因构造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1476238/

相关文章:

c++ - 如何从构造函数返回错误代码?

JavaFX 异常

android-studio - Android studio : I am getting the following error "java.util.NoSuchElementException". 但是,我无法追踪错误

java - Java 泛型的 Number.valueOf() 实现

java - 如何使用 JAXB/XJC 将 XML 文件转换为对象?

java - 需要帮助将 awt 应用程序与 MS Access 数据库连接

.net - 为什么发生异常时不显示stacktrace?

java - hibernate 不关闭连接

java - JUnit 断言 : make the assertion between floats

java - 在 Jenkins 上运行 Selenium 测试