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
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'.
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.
two solutions:
reproduce the exception on emulator. the wireless toolkit and Netbeans will print stack traces on your computer.
use a Symbian device.
Prior to the Feature Pack 2 of Series 60 3rd edition, Symbian handsets use the Sun Hotspot java virtual machine. It was adapted to Symbian OS by linking it to a partial implementation of the C standard library.
This allowed Symbian to create a C++ program called redirector, which was capable of capturing the VM standard output and standard error, including java exception stack traces.
the c++ redirector was never upgraded for version 9 of Symbian OS. Instead, a "redirect://" GCF protocol was introduced into the VM,
From a separate MIDlet, open an InputStream from the connection returned by Connector.open("redirect://", Connector.READ); you can then capture exception stack traces on Symbian phones.
EDIT : "redirect://" is back in Series60 5th edition and "redirect://test" should work on Series60 3rd edition feature pack 2
I've created a tool that can be used to log proper stack traces also in CLDC. Check it out at http://jarrut.sourceforge.net. It's still very new and it might have some rough edges, but it works for me and I couldn't imagine developing MIDlets without it anymore. Best way to use it is to combine it with microlog.
Unfotunately the tool currently requires CLDC 1.1 so it might not solve the problem for the original poster.
I don't thing there is a way to do that in CLDC 1.0. However, on some devices/OSes the underlying Exception class could be providing a way for accessing the stack trace (think newer CLDC versions). Just inspect the exception instance at runtime using reflection to see what members it exposes on your target platforms. You could then write some code that will be able to safely extract the stack trace on the platforms that offer such information.