一个例外的属性
message和
cause。该消息是一种描述,或多或少准确地告诉人类读者出了什么问题。的
cause是不同的东西:它是,如果有的话,另一个(嵌套)
Throwable。
如果我们使用这样的自定义异常,通常会使用该概念:
catch(IOException e) { throw new ApplicationException("Failed on reading file soandso", e); // ^ Message ^ Cause}编辑-响应@djangofans评论。
标准是嵌套表达式(原因)也将与其堆栈跟踪一起打印。
运行这个小应用程序
public class Exceptions { public static void main(String[] args) { Exception r = new RuntimeException("Some message"); throw new RuntimeException("Some other message", r); }}将输出
Exception in thread "main" java.lang.RuntimeException: Some other message at Exceptions.main(Exceptions.java:4) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: java.lang.RuntimeException: Some message at Exceptions.main(Exceptions.java:3) ... 5 more
这两个消息都包括在内。



