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

前端 未结 5 1343
野的像风
野的像风 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 13:58

    AFAIK there is no way to get the stack trace as a string value, unless a specific platform provides a means to override the default System.err stream. On the BlackBerry platform, it throws out the stack trace on catch(Exception) in order to save memory, however it doesn't do this on catch(Throwable) and gives access to the stack trace through the device event log.

    What I've ended up doing is catching Throwable rather than Exception at the last possible moment and printing the stack trace from there. This of course has the danger that you're also catching java.lang.Error which isn't very good, especially if its OutOfMemoryError, although a call to System.gc() before printing the stack trace seems to reduce the risk and we haven't had any problems with it.

    I'd look at whatever platform you're targeting and see if they give access to System.err somewhere. You can always hook up a debugger and it should appear on the console output, although it sounds like you're after getting stack traces 'in the field'.

提交回复
热议问题