And*_*s_D 66
Exception具有属性message和cause.该消息是一种描述,或多或少地告诉人类读者,出了什么问题.这cause是不同的东西:如果可用的话,它是另一个(嵌套的)Throwable.
如果我们使用这样的自定义异常,则经常使用该概念:
catch(IOException e) {
throw new ApplicationException("Failed on reading file soandso", e);
// ^ Message ^ Cause
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 响应@djangofans评论.
标准是嵌套表达式(原因)也用它的堆栈跟踪打印.
运行这个小应用程序
public class Exceptions {
public static void main(String[] args) {
Exception r = new RuntimeException("Some message");
throw new RuntimeException("Some other message", r);
}
}
Run Code Online (Sandbox Code Playgroud)
将输出
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
Run Code Online (Sandbox Code Playgroud)
两条消息都包含在内.
| 归档时间: |
|
| 查看次数: |
47059 次 |
| 最近记录: |