Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

java-me

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 there any way to obtain the text of a stack trace in JavaME as a String?

Update:

I should mention that I'm restricted to CLDC 1.0

like image 219
izb Avatar asked Oct 22 '08 12:10

izb


People also ask

How do I read an error stack trace?

To read this stack trace, start at the top with the Exception's type - ArithmeticException and message The denominator must not be zero . This gives an idea of what went wrong, but to discover what code caused the Exception, skip down the stack trace looking for something in the package com.

How do I print a stack trace for exceptions?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

How do I print a string stack trace?

Example: Convert stack trace to a stringIn the catch block, we use StringWriter and PrintWriter to print any given output to a string. We then print the stack trace using printStackTrace() method of the exception and write it in the writer. Then, we simply convert it to string using toString() method.

How can I get the current stack trace in Java?

We can obtain a stack trace from a thread by calling the getStackTrace() method on the Thread instance. It returns an array of StackTraceElement, from which details about stack frames of the thread can be found.


2 Answers

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

like image 195
michael aubert Avatar answered Nov 01 '22 10:11

michael aubert


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

like image 22
roryf Avatar answered Nov 01 '22 11:11

roryf