0

I have already read "Java: Why don't the PrintWriter or PrintStream classes throw exception?" but I am not interested in the reasons/excuses why those classes do not throw exceptions on output errors.

What I want to know is how to actually make them throw exceptions.

Or what else I can do with the least amount of work in order to get a replacement class for PrintStream which does the same but checks for errors and throws exceptions.

The obvious way would be to implement a wrapper class for PrintStream providing methods of the same names which call the corresponding PrintStream method, check for errors, and throw an exception if an error was detected.

However, this seems to be an awful lot of work, because PrintStream has a lot of methods.

So I wonder whether there is a simpler way.

Or perhaps someone else has already done the work if there was no other way around the obvious approach?

I find it hard to believe I would be the first one who considers immediately checking for output errors to be a good idea when writing to some output stream, instead of happily continuing in case of output errors as if nothing had happened.

  • There is a `checkError` method on `PrintStream` if that helps... – BretC Mar 06 '19 at 17:17
  • I know, but thanks for the reminder anyway. What I actually want is that the methods call checkError() themselves and throw an IOException if an error has been detected, instead of leaving that task to the caller (because this is exactly that kind of tedious code what I would like to avoid). – Guenther Brunthaler Mar 06 '19 at 17:23
  • As `IOException` is a checked exception, you won't be able to override a `PrintStream` and get it to throw those because they are checked exceptions :( You could throw an unchecked exception or wrap a `PrintStream` in a new class (but obviously then you won't be able to use it in place of a `PrintStream` :( :( – BretC Mar 06 '19 at 17:26
  • I have no problem throwing some different (and unchecked) exception, IOException just seemed so appropriate at first glance. I just want to do something like System.out.println() except that it automatically calls checkError() internally and throws any kind of exception in case of an output error. – Guenther Brunthaler Mar 06 '19 at 17:30
  • Your last comment makes a lot more more sense than your question. So my question for you: what is the real, actual problem you are facing? Show some actual code with the Printwriter you have and the behavior you need. I am confident there is a way to get it done without rewriting/redesigning or forking Java ;) – kai Mar 06 '19 at 17:34
  • @kai The problem is this: I want my program to write its results to standard output, using the same set of methods as provided by PrintStream, except that I do consider output errors to be terminal. I do not want my program to continue outputting text if the output operation actually failed. Instead, the program shall terminate with an error message (if possible) and with a return code indicating failure. – Guenther Brunthaler Mar 06 '19 at 17:39
  • simply provide a some code that shows your problem and we will see.. ;) – kai Mar 06 '19 at 17:41
  • @kai think of 500 calls to System.out.println() in sequence, every one outputting the result of some extremely expensive calculation. If everything goes right, it takes 2 days to process all 500 calls. So obviously you do not want to continue the expensive calculation once it becomes clear that output does not work at all. Therefore, after every single of those 500 lines, you would have to add a line which calls checkError() and terminates the program prematurely if an error was detected. That makes 1000 lines instead of 500. I want to avoid that. – Guenther Brunthaler Mar 06 '19 at 17:46
  • A simple method solves the problem you describe in your last comment as far as that comment is concerned. But please have a look at https://stackoverflow.com/help/how-to-ask and describe your **real** problem **in your question** with some **code** and I am confident you get some solution. – kai Mar 06 '19 at 17:56
  • @kai I have changed the wording of my question and hope that makes it clearer what I want to know. – Guenther Brunthaler Mar 06 '19 at 18:14

0 Answers0