我可以看到一个例外,我可以打印<代码>e.getCause(,但总是null
。
难道我需要把它放在某个地方,还是遗漏了什么东西,什么是造成失败的原因?
我可以看到一个例外,我可以打印<代码>e.getCause(,但总是null
。
难道我需要把它放在某个地方,还是遗漏了什么东西,什么是造成失败的原因?
属性<编码>message和cause
。 该信息是一种描述,更确切地说是,什么是错的。
The concept is often used if we use custom exceptions like this:
catch(IOException e) {
throw new ApplicationException("Failed on reading file soandso", e);
// ^ Message ^ Cause
}
答复 s comment:
The standard is that the nested expression (the cause) is printed with its stack trace too.
很少应用
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
Both messages are included.
原因通常在例外情况的构造中确定。 看见public Statelessness(String information, Throwable causes)。
如果在建筑商中固定,请打电话initCause()。
<+>Exception有条码的构造。 可浏览。 你们需要把这些建筑商或建筑商称作超级建筑商的习俗例外类别。
<>strong>getCause——如果原因不存在或未知,则归还造成这种 throw落或无效的原因。 (原因就是造成这种沉积的沉积,以推倒)
阅读 Java文件:getCause
If you just want to get rid of null as the cause then override toString() method of your CustomException class that extends Exception class.
public class CustomException extends Exception {
private static final long serialVersionUID = 9355648L;
public CustomException(String message, Throwable cause) {
super(message, cause);
}
@Override
public String toString() {
return "Business Exception";
}
}
以及
throw new CustomException("Stopping further processing.", new CustomException("stale message"));
产出如下:
Stopping further processing. Cause : Business Exception
a 包括:
} catch (CustomException ce) {
System.out.print(ce.getMessage());
System.out.println(" Cause : " + ce.getCause());
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...