• Post Reply Bookmark Topic Watch Topic
  • New Topic

Grabbing and using system.out  RSS feed

 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Rusty Smythe
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!