Jak przechowywać printStackTrace w łańcuchu [duplikat]
To pytanie ma już odpowiedź tutaj:
- Jak mogę przekonwertować ślad stosu na ciąg znaków? 26 odpowiedzi
Jak mogę pobrać e.printStackTrace()
i zapisać go do zmiennej String
?
Chcę użyć łańcucha wygenerowanego przez e.printStackTrace()
później w moim programie.
Wciąż jestem nowy w Javie, więc nie jestem zbyt zaznajomiony z StringWriter
, że myślę
będzie rozwiązaniem. Lub jeśli masz jakieś inne pomysły, daj mi znać. Dzięki
8 answers
Coś w stylu
StringWriter errors = new StringWriter();
ex.printStackTrace(new PrintWriter(errors));
return errors.toString();
/ Align = "left" /
Odpowiednia dokumentacja:
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-06-19 07:43:15
Guava ułatwia to z Throwables.getStackTraceAsString(Throwable):
Exception e = ...
String stackTrace = Throwables.getStackTraceAsString(e);
Wewnętrznie, to robi to, co sugeruje @Zach L.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-11 05:40:17
Musisz użyć metody getStackTrace ()
zamiast printStackTrace()
. Oto dobry przykład :
import java.io.*;
/**
* Simple utilities to return the stack trace of an
* exception as a String.
*/
public final class StackTraceUtil {
public static String getStackTrace(Throwable aThrowable) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
aThrowable.printStackTrace(printWriter);
return result.toString();
}
/**
* Defines a custom format for the stack trace as String.
*/
public static String getCustomStackTrace(Throwable aThrowable) {
//add the class name and any message passed to constructor
final StringBuilder result = new StringBuilder( "BOO-BOO: " );
result.append(aThrowable.toString());
final String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE);
//add each element of the stack trace
for (StackTraceElement element : aThrowable.getStackTrace() ){
result.append( element );
result.append( NEW_LINE );
}
return result.toString();
}
/** Demonstrate output. */
public static void main (String... aArguments){
final Throwable throwable = new IllegalArgumentException("Blah");
System.out.println( getStackTrace(throwable) );
System.out.println( getCustomStackTrace(throwable) );
}
}
Wzdłuż linii Guava, Apache Commons Lang mA ExceptionUtils.getFullStackTrace
w org.apache.commons.lang.exception
. z wcześniejszej odpowiedzi na StackOverflow.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:31
Możesz użyć {[1] } z klasy Apache Commons 3 org.apache.commons.lang3.exception.ExceptionUtils
.
Http://commons.apache.org/proper/commons-lang/
Wyjątek.getStackTrace (Throwable t)
Przykład kodu:
try {
// your code here
} catch(Exception e) {
String s = ExceptionUtils.getStackTrace(e);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-07 23:24:10
StackTraceElement[] stack = new Exception().getStackTrace();
String theTrace = "";
for(StackTraceElement line : stack)
{
theTrace += line.toString();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-27 03:59:01
Użyj Apache commons-lang3 lib
import org.apache.commons.lang3.exception.ExceptionUtils;
//...
String[] ss = ExceptionUtils.getRootCauseStackTrace(e);
logger.error(StringUtils.join(ss, System.lineSeparator()));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-17 16:32:11
call: getStackTraceAsString(sqlEx)
public String getStackTraceAsString(Exception exc)
{
String stackTrace = "*** Error in getStackTraceAsString()";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( baos );
exc.printStackTrace(ps);
try {
stackTrace = baos.toString( "UTF8" ); // charsetName e.g. ISO-8859-1
}
catch( UnsupportedEncodingException ex )
{
Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
}
ps.close();
try {
baos.close();
}
catch( IOException ex )
{
Logger.getLogger(sss.class.getName()).log(Level.SEVERE, null, ex);
}
return stackTrace;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-05-09 16:18:43