This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Is it possible to rethrow printStackTrace()?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Is there a way to convert printStream into String object.
Appreciate any help on this.
[This message has been edited by Sridhar Ramakrishnan (edited April 19, 2000).]
[This message has been edited by Sridhar Ramakrishnan (edited April 19, 2000).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean about "re-throw printStackTrace()". You can re-throw the exception - the stack trace will go with it, and be available when you call e.printStackTrace(). You can also experiment with e.fillInStackTrace() - I've never been really clear on what exactly it does, but it's supposed to be of use when you re-throw an exception.
As for your second question: Is there a way to convert printStream into String object? In general, it's not possible with a pre-existing PrintStream, unless the PrintStream was specifically set up for such a conversion. This takes some work, but can be done.
The annoying thing about e.printStackTrace(), as you've evidently discovered, is that it always sends its output to System.err. If you want to capture the stack trace for any other use, it's not set up for that. But we can work around that:

This may seem a rather convoluted way to get a String from a printStackTrace(), but it's the best I can come up with. The ByteArrayOutputStream is the only OutputStream I could find which can conver to to a String - there's also the StringWriter, but there's no way I could find to connect an OutputStream to a Writer with the Writer on the inside.
One possible problem I see with this code: It's not thread-safe unless all other code that writes to System.err also uses a synchronized(System.err) {} block. If another thread tries to write to System.err between the System.setErr(newErr) and the System.seetErr(oldErr) without using an appropriate synchronized block, it will write to our string instead. Note that any uncaught exception will write to System.err, and there's no way we can put that code into a synchronized block. Of course, in the case of an uncaught exception the program's about to exit anyway, so we probably don't really care much about our string at this point.
 
Sridhar Ramakrishnan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
The idea was to capture the exception, pass it on to a html for display and also to log onto an errorlog.
The best I could do was to get e.toString() which returned the error message but not the class/method location where the exception occurred.
I could not log the message returned by e.printStackTrace().
Converting printStream into a String object:
I think, the approach suggested by you should work because the exception gets logged anyway and that's what was needed.
And Thank you for the really quick response.
Sridhar
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! I overlooked the overloaded method printStackTrace(PrintStream), which makes the whole problem simpler because you don't have to futz around with System.err and synchronized code. Here's the simpler version:

I remember finding printStackTrace() really annoying when I first tried to do this, before I knew anything about IO streams and such, so now I'm determined to find a solution. I think this does it, aside from maybe optimizing for speed.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative to Jim's solution uses the other printStackTrace() method (I was going to post this yesterday but went home instead ).

Does anyone know of efficiency issues wrt PrintStream/ ByteArrayOutputStream versus PrintWriter/ StringWriter ?
Another issue is that you can lose the line numbers through a JIT, although HotSpot does not suffer from this due to the way it works.
 
Sridhar Ramakrishnan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried both solutions. It worked!
Thanks to you, Jim and Gary.

 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Jim,
why not use Throwable.printStackTrace(PrintStream out) ?
chantal
y posted by Jim Yingst:
[B]I'm not sure what you mean about "re-throw printStackTrace()". You can re-throw the exception - the stack trace will go with it, and be available when you call e.printStackTrace(). You can also experiment with e.fillInStackTrace() - I've never been really clear on what exactly it does, but it's supposed to be of use when you re-throw an exception.
As for your second question: Is there a way to convert printStream into String object? In general, it's not possible with a pre-existing PrintStream, unless the PrintStream was specifically set up for such a conversion. This takes some work, but can be done.
The annoying thing about e.printStackTrace(), as you've evidently discovered, is that it always sends its output to System.err. If you want to capture the stack trace for any other use, it's not set up for that. But we can work around that:

This may seem a rather convoluted way to get a String from a printStackTrace(), but it's the best I can come up with. The ByteArrayOutputStream is the only OutputStream I could find which can conver to to a String - there's also the StringWriter, but there's no way I could find to connect an OutputStream to a Writer with the Writer on the inside.
One possible problem I see with this code: It's not thread-safe unless all other code that writes to System.err also uses a synchronized(System.err) {} block. If another thread tries to write to System.err between the System.setErr(newErr) and the System.seetErr(oldErr) without using an appropriate synchronized block, it will write to our string instead. Note that any uncaught exception will write to System.err, and there's no way we can put that code into a synchronized block. Of course, in the case of an uncaught exception the program's about to exit anyway, so we probably don't really care much about our string at this point.[/B]
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I didn't read all the messages at once.
 
See where your hand is? Not there. It's next to this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic