How to get the text of an exception stack trace in Java ME?

前端 未结 5 1316
野的像风
野的像风 2021-02-15 13:20

In regular Java, you can get the text of a stack trace by passing a PrintWriter to printStackTrace. I have a feeling I know the answer to this (i.e. \"No\") but,

Is ther

5条回答
  •  野的像风
    2021-02-15 14:03

    You can have the PrintWriter write to a ByteArrayOutputStream and reconstruct the String from the bytes.

    try{
        throw new Exception("Message");     
    } catch (Exception ex){
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ex.printStackTrace(new PrintStream(out));
        System.out.println(new String(out.toByteArray()));
    }
    

    It's not pretty, but it should work pretty much everywhere. Before you try the above, make sure you don't have access to [Throwable#getStackTrace](http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Throwable.html#getStackTrace()), Eclipse claims it's available in CDC/Foundation 1.1, but that doesn't say anything about other profiles.

提交回复
热议问题