How can I convert a stack trace to a string?

前端 未结 30 1171
再見小時候
再見小時候 2020-11-22 14:51

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?

相关标签:
30条回答
  • 2020-11-22 15:25

    The following code allows you to get the entire stackTrace with a String format, without using APIs like log4J or even java.util.Logger:

    catch (Exception e) {
        StackTraceElement[] stack = e.getStackTrace();
        String exception = "";
        for (StackTraceElement s : stack) {
            exception = exception + s.toString() + "\n\t\t";
        }
        System.out.println(exception);
        // then you can send the exception string to a external file.
    }
    
    0 讨论(0)
  • 2020-11-22 15:25

    My oneliner to convert stack trace to the enclosed multi-line string:

    Stream.of(e.getStackTrace()).map((a) -> a.toString()).collect(Collectors.joining("\n", "[", "]"))
    

    Easy to pass to the logger "as is".

    0 讨论(0)
  • 2020-11-22 15:26

    If you are developing for Android, a far easier way is to use this:

    import android.util.Log;
    
    String stackTrace = Log.getStackTraceString(exception); 
    

    The format is the same as getStacktrace, for e.g.

    09-24 16:09:07.042: I/System.out(4844): java.lang.NullPointerException
    09-24 16:09:07.042: I/System.out(4844):   at com.temp.ttscancel.MainActivity.onCreate(MainActivity.java:43)
    09-24 16:09:07.042: I/System.out(4844):   at android.app.Activity.performCreate(Activity.java:5248)
    09-24 16:09:07.043: I/System.out(4844):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
    09-24 16:09:07.043: I/System.out(4844):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
    09-24 16:09:07.043: I/System.out(4844):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
    09-24 16:09:07.043: I/System.out(4844):   at android.app.ActivityThread.access$800(ActivityThread.java:139)
    09-24 16:09:07.043: I/System.out(4844):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
    09-24 16:09:07.043: I/System.out(4844):   at android.os.Handler.dispatchMessage(Handler.java:102)
    09-24 16:09:07.043: I/System.out(4844):   at android.os.Looper.loop(Looper.java:136)
    09-24 16:09:07.044: I/System.out(4844):   at android.app.ActivityThread.main(ActivityThread.java:5097)
    09-24 16:09:07.044: I/System.out(4844):   at java.lang.reflect.Method.invokeNative(Native Method)
    09-24 16:09:07.044: I/System.out(4844):   at java.lang.reflect.Method.invoke(Method.java:515)
    09-24 16:09:07.044: I/System.out(4844):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    09-24 16:09:07.044: I/System.out(4844):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    
    0 讨论(0)
  • 2020-11-22 15:27

    For me the cleanest and easiest way was:

    import java.util.Arrays;
    Arrays.toString(e.getStackTrace());
    
    0 讨论(0)
  • 2020-11-22 15:27
    private String getCurrentStackTraceString() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        return Arrays.stream(stackTrace).map(StackTraceElement::toString)
                .collect(Collectors.joining("\n"));
    }
    
    0 讨论(0)
  • 2020-11-22 15:27

    The solution is to convert the stackTrace of array to string data type. See the following example:

    import java.util.Arrays;
    
    try{
    
    }catch(Exception ex){
        String stack = Arrays.toString(ex.getStackTrace());
        System.out.println("stack "+ stack);
    }
    
    0 讨论(0)
提交回复
热议问题