Java-定义多个自定义异常,而无需重写每个(干式)

我想在我的代码中有几个自定义错误(扩展RuntimeException),但是似乎我每次都需要写出相同的代码-这样做无需太多重复就可以了吗?

例如,在下面的代码中,如果我还想产生一个下溢错误,或者可能还有其他一些错误,我是否必须复制并粘贴此代码,以将“ OverflowError”替换为任何我想调用新异常的东西?有什么办法可以使这种“烘干机”?

class OverflowError extends RuntimeException {
    private static final long serialVersionUID = 1L; //VS Code says I need it and I don't know what this line does,but that's another question for another day

    public OverlowError(String message) {
        super(message);
    }
}

似乎如果我在内部没有代码的情况下扩展RuntimeException,它不会继承构造函数,因此由于意外的参数而无法正常工作。

jiandan103 回答:Java-定义多个自定义异常,而无需重写每个(干式)

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3163456.html

大家都在问