简体   繁体   English

在throwable对象中重载getCause()方法

[英]Overloading the getCause() method in a throwable object

How would one go about overloading the getCause() method in a throwable object ? 如何在throwable对象中重载getCause()方法? I have the following but it doesn't seem to work as it says that it cannot be overloaded with a string. 我有以下但它似乎没有工作,因为它说它不能重载字符串。

public class MyException extends RuntimeException   {
String cause;
MyException(String s)   {
    cause = s;
}
@Overwrite public String getCause()    {
    return cause;
}

It is illegal to have two methods that only differ in their return type. 拥有两种只返回类型不同的方法是违法的。 Suppose someone wrote: 假设有人写道:

Object obj = myException.getCause();

That is perfectly legal java, and the compiler has no way to figure out if it's the String version or the Throwable version. 这是完全合法的java,编译器无法弄清楚它是String版本还是Throwable版本。

Likewise you can't replace the superclass signature since this is also perfectly legal: 同样,您无法替换超类签名,因为这也是完全合法的:

Throwable t = new MyException();
Throwable t0 = t.getCause();
//Returns String?!?!?!?

Accepted answer clears the point : 接受的答案清除了这一点

It is illegal to have two methods that only differ in their return type 拥有两种只返回类型不同的方法是违法的

But if you have a situation where, getCause() should return the custom cause in MyException , in case original cause is null. 但是如果你遇到这种情况, getCause()应该在MyException返回自定义原因,以防原始原因为null。

In that case, you can use initCause() to set the cause and override toString() method. 在这种情况下,您可以使用initCause()来设置原因并覆盖toString()方法。 So, when getCause() method will be called on object of MyException , it will show the message from customCause instead of null. 因此,当在MyException对象上调用getCause()方法时,它将显示来自customCause而不是null的消息。

What is the use: In legacy system, if you have used getCause() on MyException object while logging, and now you want to add the custom cause to it without changing lot of code, here is the way. 有什么用:在遗留系统中,如果你在登录时在MyException对象上使用了getCause() ,现在你想在不改变代码的情况下添加自定义原因,这就是方法。

    public class MyException extends RuntimeException {
        String customCause;

        MyException(String s) {
            super(s);
            customCause = s;
        }

        @Override
        public synchronized Throwable getCause() {
            if (super.getCause() != null) {
                return this;
            } else {
                this.initCause(new Throwable(customCause));
                return this;
            }
        }

        @Override
        public String toString() {
            String s = getClass().getName();
            String message = getLocalizedMessage();
            if (message == null) {
                message = customCause;
            }
            return (message != null) ? (s + ": " + message) : s;
        }
    }

References: https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable) https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html 参考文献: https//docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable) https://docs.oracle.com/javase/7/文档/ API / JAVA / LANG / Throwable.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM