如何在Java ME中获取异常堆栈跟踪的文本? [英] How to get the text of an exception stack trace in Java ME?

查看:99
本文介绍了如何在Java ME中获取异常堆栈跟踪的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在常规Java中,您可以通过将PrintWriter传递给printStackTrace来获取堆栈跟踪的文本。我有一种感觉我知道答案(即否)但是,

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,

有没有办法在JavaME中获取堆栈跟踪的文本作为字符串?

Is there any way to obtain the text of a stack trace in JavaME as a String?

更新:

我应该提到我只限于CLDC 1.0

I should mention that I'm restricted to CLDC 1.0

推荐答案

AFAIK除非特定平台提供覆盖方法,否则无法将堆栈跟踪作为字符串值默认的System.err流。在BlackBerry平台上,它会抛出 catch(Exception)上的堆栈跟踪以节省内存,但是它不会在 catch上执行此操作(Throwable)并通过设备事件日志提供对堆栈跟踪的访问。

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.

我最后做的是在最后可能的时刻捕获Throwable而不是Exception并从那里打印堆栈跟踪。这当然有一个危险,你也抓住 java.lang.Error ,这不是很好,特别是如果它的 OutOfMemoryError ,虽然在打印堆栈跟踪之前调用 System.gc()似乎可以降低风险,但我们没有遇到任何问题。

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.

我会查看您所定位的平台,看看他们是否可以访问某个地方的System.err。你总是可以连接一个调试器,它应该出现在控制台输出上,虽然听起来就像你在字段中获得堆栈跟踪'。

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'.

这篇关于如何在Java ME中获取异常堆栈跟踪的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆