[英]Java exception handling get console error message
I want to get error message using java when exception are generated. 在生成异常时,我想使用Java来获取错误消息。
now I have java code with following scenario: 现在我有以下情况的Java代码:
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e)
}
}
method second(){
try{
my code
}catch(Exception e){
throw new Exception("Exception generate in second method",e);
}
}
now when the first method execute then I get only "Exception generate in second method" message but there is some other message printed on console by java so how to get that console error message. 现在,当第一个方法执行时,我只会收到“第二个方法中生成异常”消息,但是java会在控制台上打印一些其他消息,以便了解如何获取该控制台错误消息。
Note: I have already try with e.getMessage(); 注意:我已经尝试使用e.getMessage();。 and e.printStackTrace();
和e.printStackTrace();
Every exception has a cause that you can get with getCause()
. 每个异常都有一个可以通过
getCause()
获得的原因。 You can go recursively down them until you get to the root cause. 您可以递归查看它们,直到找到根本原因。 Here is your example with a utility that dumps the exception with all its causes like the console does.
这是您的示例,其中包含一个实用程序,该实用程序会像控制台一样转储异常的所有原因。
private void first() {
try {
second();
} catch (Exception ex) {
Log.e("CATCH", getExceptionDump(ex));
}
}
private void second() {
try {
throw new UnsupportedOperationException("We don't do this.");
} catch (Exception ex) {
throw new RuntimeException("Exception in second()", ex);
}
}
private String getExceptionDump(Exception ex) {
StringBuilder result = new StringBuilder();
for (Throwable cause = ex; cause != null; cause = cause.getCause()) {
if (result.length() > 0)
result.append("Caused by: ");
result.append(cause.getClass().getName());
result.append(": ");
result.append(cause.getMessage());
result.append("\n");
for (StackTraceElement element: cause.getStackTrace()) {
result.append("\tat ");
result.append(element.getMethodName());
result.append("(");
result.append(element.getFileName());
result.append(":");
result.append(element.getLineNumber());
result.append(")\n");
}
}
return result.toString();
}
The message in the Exception constructor argument is not printed in the exception detail. 异常构造函数参数中的消息未打印在异常详细信息中。 You can simply use this code to print the message :
您可以简单地使用以下代码来打印消息:
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e.getMessage())
}
}
Hope this solves your problem 希望这能解决您的问题
Why you cannot use print stack trace ? 为什么不能使用打印堆栈跟踪?
Because A throwable contains a snapshot of the execution stack of its thread at the time it was created.
因为
A throwable contains a snapshot of the execution stack of its thread at the time it was created.
(see Throwable ) (请参阅Throwable )
It implies that, if you want to print the stack trace you need to use the printStackTrace()
method BUT in your second method ! 这意味着,如果要打印堆栈跟踪,则需要在第二个方法中使用
printStackTrace()
方法BUT!
method second(){
try {
my code
} catch(Exception e) {
e.printStackTrace();
throw new Exception("Exception generate in second method",e);
}
}
Or using a the tricky method setStackTrace
and using the printStackTrace()
in first 或者使用棘手的方法
setStackTrace
并首先使用printStackTrace()
method second(){
try {
my code
} catch(Exception e) {
Exception ex = new Exception("Exception generate in second method",e);
ex.setStackTrace(e);
throw ex;
}
}
method first(){
try {
second();
} catch(Exception e) {
e.printStackTrace();
}
}
You can print the cause of the exception you get. 您可以打印得到的异常原因。 Try this:
尝试这个:
method first(){
try{
second();
}catch(Exception e){
System.out.println("Error:> "+e);
if (e.getCause() != null) {
System.out.println("Cause:> " + e.getCause());
}
}
}
I believe this is the console message you want to achieve: 我相信这是您要实现的控制台消息:
Error:> java.lang.Exception: Exception generate in second method
Try this code, when the catch block of the second method throws an exception the second method should declare it as throws or put a nested try catch within the catch block. 尝试使用此代码,当第二个方法的catch块引发异常时,第二个方法应将其声明为throws或在catch块内放入嵌套的try catch。
The exception is propagated to the first() method which is handled by its catch block. 异常将传播到由其catch块处理的first()方法。
public class Test {
public void first() {
try {
second();
} catch (Exception e) {
System.out.println("Error:> " + e);
}
}
public void second() throws Exception {
try {
throw new Exception();
} catch (Exception e) {
throw new Exception("Exception generate in second method", e);
}
}
public static void main(String ars[]) {
Test test = new Test();
test.first();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.