简体   繁体   English

Throwable类的getCause()函数(在Java中)未按预期工作

[英]getCause() function of Throwable class (in Java) is not working as expected

I am getting null while calling the getCause function of Throwable . 我在调用ThrowablegetCause函数时得到null。

package com.salman.demo;
public class MyClass {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            NormalClass.testIt();
        } catch (Throwable th) {
            System.out.println(th.getCause());
        }
    }
}


package com.salman.demo;

import com.salman.exceptions.ValidationException;

public class NormalClass {
    public static void testIt() throws ValidationException {
        System.out.println("Inside testIt funtion.");

        throw new ValidationException("Validation Exception..");
    }
}

On running MyClass , it prints following output 在运行MyClass ,它将输出以下输出

Inside testIt funtion.
null

however on debugging this, I can see value of cause private variable is set to ValidationException which is expected but while calling getter of that private field returns null. 但是,在调试此代码时,我可以看到cause私有变量的值设置为ValidationException ,这是预期的,但是在调用该私有字段的getter时会返回null。

The ValidationException you've thrown will be th in your calling code... there's no cause for that ValidationException . ValidationException你扔将是th在调用代码...有这个问题并没有引起ValidationException The point of getCause() is for one exception to be able to refer to another one as an underlying cause - but there's only one exception in your case. getCause()的要点是,一个异常能够将另一异常作为根本原因-但您的情况只有一个异常。

The fact that you see the private cause variable's value being the ValidationException is entirely in line with how that field is documented (emphasis mine): 您看到私有cause变量的值是ValidationException的事实与该字段的记录方式完全一致(强调我的意思):

The throwable that caused this throwable to get thrown, or null if throwable was not caused by another throwable, or if the causative throwable is unknown. 导致引发此throwable的throwable;如果throwable不是由另一个throwable引起的,或者如果引起原因的throwable未知,则为null。 If this field is equal to this throwable itself, it indicates that the cause of this throwable has not yet been initialized. 如果此字段等于此throwable本身,则表明此throwable的原因尚未初始化。

And that's why getCause() is implemented as: 这就是为什么getCause()实现为:

return (cause==this ? null : cause);

If you want to see the intended chaining in effect, you could create two exceptions, one chained to the other: 如果您希望看到预期的链接有效,则可以创建两个异常,一个链接到另一个:

throw new ValidationException("Validation Exception..",
    new IllegalArgumentException("Bang!"));

Now calling getCause() on the ValidationException will return the IllegalArgumentException . 现在在ValidationException上调用getCause()将返回IllegalArgumentException

You probably just want to change your calling code to log th instead of th.getCause() though. 您可能只想更改您的调用代码以登录th而不是th.getCause()

Your exception is the first one in the chain, so it does not have a cause. 您的例外是该链中的第一个例外,因此没有原因。

If you want to test, it you need to have chain of exception, so you thorw one catch it and throw new one which is constructed using the previous. 如果要测试,则需要具有异常链,因此请阻止其中的一个并抛出使用前一个构造的新异常。

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

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