简体   繁体   English

堆栈跟踪为字符串

[英]Stack trace as String

How do I get full exception message in Java? 如何在Java中获得完整的异常消息?

I am creating a Java Application. 我正在创建一个Java应用程序。 If there is a runtime exception, a JDialog with a JTextArea will pop up. 如果存在运行时异常,则会弹出带有JTextAreaJDialog I tried to make a runtime exception in my application, but the text area showing the exception message looks like this: 我试图在我的应用程序中生成运行时异常,但显示异常消息的文本区域如下所示:

java.lang.ClassNotFoundException: com.app.Application

However, I want my text area to show something like: 但是,我希望我的文本区域显示如下:

在此输入图像描述

Here is some part of my code surrounded by the try and catch : 这是我的代码的一部分被trycatch包围:

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

I've tried to use e.getLocalizedMessage() and e.getMessage() , but neither of these work. 我试过使用e.getLocalizedMessage()e.getMessage() ,但这些都不起作用。

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()

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); 
}

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();

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

尝试使用e.printStackTrace() ,它将使用提到的行打印您的异常的跟踪

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 那么你会惊讶地发现你在你拍摄的照片上看到的东西

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM