• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Grabbing and using system.out

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Apologies if this belongs in beginner - I know it is not terribly advanced, but it is not for rank amateurs either. Please move if necessary)

I am working with a supplier class that improperly deals with exceptions. Instead of throwing them, it catches its own problems and prints them to system.out.

(For example - one of the things it does is parse a file at a given URL. If it can't find the URL, it will generate either a UnknownHostException or a FileNotFoundException, depending on where it breaks. It prints either one to System.out.)

I would like to somehow grab those exceptions and use them as tests in my client class - so that if the file loading fails, I can tell the user which kind of problem my supplier class encountered.

I know I can use System.setErr and System.setOut to redirect that output to a Printstream. But how can I test what was recently added to it.

My desire goes like this in imagino-code:

myPrintStream.flush() //To clear it
parseFileAtURL( http://www.javaranch.com );
if (myPrintStream.lastAddition().contains( "UnknownHost"))
{ myWindowPrintMethod( "The URL you gave was not from a known host.") }
else if ( myPrintStream.lastAddition().contains( "FileNotFound") )
{ myWindowPrintMethod( "That file could not be found") }
else { continueOnWithExecution() }

How could I achieve this general idea? I know the good thing to do would be to change the supplier code but I am curious about how I could deal with it without doing so.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to create a PrintStream wrapped around a ByteArrayOutputStream, set System.out to that (saving the old one,) call your naughty API, use "new String(baos.getByteArray())" to make a String out of the ByteArrayOutputStream, test that String, and then, in a finally block, put the original System.out back. Make sense?
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
You want to create a PrintStream wrapped around a ByteArrayOutputStream, set System.out to that (saving the old one,) call your naughty API, use "new String(baos.getByteArray())" to make a String out of the ByteArrayOutputStream, test that String, and then, in a finally block, put the original System.out back. Make sense?



Hey, that worked! And I learned about B.A.O.String's and PrintSterams in the process. Cool, thanks!
 
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is exactly why exceptions should not be hidden and ignored.

If class A calls class B, then B must throw any errors that A should know about, otherwise Bad Things will happen and people will have to Waste Cycles Writing Code to Fix Something That Isn't Broken.

Yes, sometimes exceptions occur and the code needs to keep doing something (e.g., opening a file fails, so try to open an alternate file), but that's what "finally" is for!

... not that my rant isn't advanced Java (it isn't), but it is a good example of why "shortcuts" are not.
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rusty Smythe:
This is exactly why exceptions should not be hidden and ignored.

Yeah - the code in question was pre-written code that we were to use in an intro to java course that I am taking at my local Community College. The instructor explained to me that he had written it that way because the class doesn't cover exception catching - that comes in the second semester, so he wanted the only possible outcomes to be full success or null returns.
We weren't expected to give the level of detail in the feedback that I wanted to give, but I have a stick in my....ear about non-helpful error messages. I spent way, way to much time getting it to work right, but in the end it does (Thanks again EFH!). It makes no difference to my grade, but it is very satisfying to have it behave in an intelligent way.

 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic