Java 堆栈跟踪作为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18546842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:55:48  来源:igfitidea点击:

Stack trace as String

javaexception

提问by Jeremy

How do I get full exception message in Java?

如何在 Java 中获取完整的异常消息?

I am creating a Java Application. If there is a runtime exception, a JDialogwith a JTextAreawill pop up. I tried to make a runtime exception in my application, but the text area showing the exception message looks like this:

我正在创建一个 Java 应用程序。如果有运行时异常,会弹出一个JDialog带 a 的JTextArea。我试图在我的应用程序中创建运行时异常,但显示异常消息的文本区域如下所示:

java.lang.ClassNotFoundException: com.app.Application

However, I want my text area to show something like:

但是,我希望我的文本区域显示如下内容:

enter image description here

在此处输入图片说明

Here is some part of my code surrounded by the tryand catch:

这是我的代码的一部分,被tryand包围catch

String ex = e.toString();
this.addText(ex);

I've tried to use e.getLocalizedMessage()and e.getMessage(), but neither of these work.

我尝试使用e.getLocalizedMessage()and e.getMessage(),但这些都不起作用。

采纳答案by Narendra Pathai

You need to call the Throwable#printStackTrace(PrintWriter);

您需要致电 Throwable#printStackTrace(PrintWriter);

try{

}catch(Exception ex){
    String message = getStackTrace(ex);
}

public static String getStackTrace(final Throwable throwable) {
     final StringWriter sw = new StringWriter();
     final PrintWriter pw = new PrintWriter(sw, true);
     throwable.printStackTrace(pw);
     return sw.getBuffer().toString();
}

You can also use commons-lang-2.2.jar Apache Commons Lang library which provides this functionality:

您还可以使用提供此功能的 commons-lang-2.2.jar Apache Commons Lang 库:

public static String getStackTrace(Throwable throwable)
Gets the stack trace from a Throwable as a String.

ExceptionUtils#getStackTrace()

ExceptionUtils#getStackTrace()

回答by Onur A.

Try e.printStackTrace(), it will print trace for your exception with lines mentioned

试试e.printStackTrace(),它会用提到的行打印你的异常的跟踪

回答by Philipp Sander

This is the full exception message.

这是完整的异常消息。

If you want more information try e.printStackTrace()

如果您想了解更多信息,请尝试 e.printStackTrace()

then you will get excatly what you see on the picture you postet

然后你会得到你在发布的图片上看到的内容

回答by Rakesh Soni

if you are not using any logger(Log4j or oracle logger api) then you can use the below statement to print the full exception message

如果您没有使用任何记录器(Log4j 或 oracle logger api),那么您可以使用以下语句打印完整的异常消息

try{
       // statement which you want to monitor 

}catch(Exception e){
    e.printStackTrace()
    // OR System.err.println(e.getStackTrace());
    // OR System.err.println(e);
    // OR System.err.println(e.getMessage());
    // OR System.err.println(e.getCause());
}

if you are using the Logger API then use the below statement

如果您使用的是 Logger API,则使用以下语句

try{

         // statement which you want to monitor 

}catch(Exception e){
  log.error(e,e); 
}

回答by Siva

e.printStackTracewill give the full stack trace

e.printStackTrace将提供完整的堆栈跟踪

回答by Henry

To get the stack trace into a string you can use these lines:

要将堆栈跟踪转换为字符串,您可以使用以下几行:

    CharArrayWriter cw = new CharArrayWriter();
    PrintWriter w = new PrintWriter(cw);
    e.printStackTrace(w);
    w.close();
    String trace = cw.toString();

回答by Jiawei Yang

public static String getStackMsg(Throwable e) {
    StringBuffer sb = new StringBuffer();
    sb.append(e.toString()).append("\n");
    StackTraceElement[] stackArray = e.getStackTrace();

    for(int i = 0; i < stackArray.length; ++i) {
        StackTraceElement element = stackArray[i];
        sb.append(element.toString() + "\n");
    }

    return sb.toString();
}