简体   繁体   English

NullPointerException:每个异常都不包含原因吗?

[英]NullPointerException: Does not every exception contain a cause?

I've tried calling this on exceptions universally and by default throughout my application: 我已经尝试在普遍的情况下调用此异常,默认情况下在我的应用程序中调用:

String causeClass = e.getCause().getClass().getName();

But I'm getting NullPointerException s. 但是我得到了NullPointerException

Is there a safe way to look for a cause and get the class name of the cause exception? 有没有一种安全的方法来查找原因并获取原因异常的类名?

As JustinKSU pointed out, sometimes there is no cause. 正如JustinKSU指出的那样,有时候没有原因。 From Throwable#getCause() : 来自Throwable#getCause()

Returns the cause of this throwable or null if the cause is nonexistent or unknown. 返回此throwable的原因,如果原因不存在或未知,则返回null。 (The cause is the throwable that caused this throwable to get thrown.) (原因是导致抛出此抛掷物的抛掷物。)

When this happens, getCause() returns null , therefore throwing a NullPointerException when you invoke getClass() . 发生这种情况时, getCause()返回null ,因此在调用getClass()时抛出NullPointerException Perhaps you can use this instead: 也许你可以用它代替:

Throwable t = e.getClause();
String causeClass = t == null ? "Unknown" : t.getClass().getName(); 

However, for debugging purposes, I find e.printStackTrace() to be much better. 但是,出于调试目的,我发现e.printStackTrace()要好得多。 This way, you can see where exactly is the exception being thrown. 这样,您可以看到抛出异常的确切位置。 Here is a typical output from printStackTrace() : 以下是printStackTrace()的典型输出:

 java.lang.NullPointerException
         at MyClass.mash(MyClass.java:9)
         at MyClass.crunch(MyClass.java:6)
         at MyClass.main(MyClass.java:3)

The stack trace is different than the cause. 堆栈跟踪与原因不同。 If you create an exception 如果您创建例外

new Exception("Something bad happened")

There is no cause. 没有理由。

If however, you create an exception passing in another exception, then there will be a cause. 但是,如果你创建了一个传递另一个异常的异常,那么就会有一个原因。 For example: 例如:

try {
    //do stuff
} catch (IOException ie) {
    throw new Exception("That's not good", ie);
} 

Then ie will be the cause. ie原因。

If you are trying to determine which class throw the exception you can do something like this: 如果您要确定哪个类抛出异常,您可以执行以下操作:

public static String getThrowingClass(Throwable t){
    StackTraceElement[] trace = t.getStackTrace();
    if(trace != null && trace.length > 0){
        return trace[0].getClassName();
    } else {
        return "UnknownClass";
    }
}

As your outcome shows, some Exception s (and some Throwable s in general) do not have a cause. 正如您的结果所示,一些Exception (以及一些一般的Throwable没有原因。 As stated in the docs : 文档中所述:

Returns the cause of this throwable or null if the cause is nonexistent or unknown. 返回此throwable的原因,如果原因不存在或未知,则返回null。 (The cause is the throwable that caused this throwable to get thrown.) (原因是导致抛出此抛掷物的抛掷物。)

As JG points out, printStackTrace() is a good alternative. 正如JG指出的那样, printStackTrace()是一个不错的选择。

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

相关问题 为什么这不会导致NullPointerException? - Why does this not cause a NullPointerException? 为什么这会导致NullPointerException? - Why does this cause a NullPointerException? 为什么此代码会导致NullPointerException? - Why does this code cause a NullPointerException? 为什么在hadoop中创建路径会导致NullPointerException? - Why does creating a Path in hadoop cause a NullPointerException? 显示/打印null值是否会导致NullPointerException - Does displaying/printing a null value cause NullPointerException 异常 org.apache.jasper.JasperException:java.lang.NullPointerException 根本原因 java.langullException.N - exception org.apache.jasper.JasperException: java.lang.NullPointerException root cause java.lang.NullPointerException 此代码如何在GridPane中查找节点的位置会导致NullPointerException? - How does this code for finding the location of a node in a GridPane cause a NullPointerException? 为什么检查hadoop中是否存在文件会导致NullPointerException? - Why does checking whether a file exists in hadoop cause a NullPointerException? 从Java调用Clojure会抛出不包含stacktrace的NullPointerException - Invoking Clojure from Java throws NullPointerException that does not contain stacktrace 为什么此异常不会导致运行时错误? - Why does this exception not cause a runtime error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM