1

I have been making drivers and have been using the BufferedReader to read in user input initialized as so

 BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

Whenever I go to read in a value say

int userAns = Integer.parseInt(input.readLine());

I will need to surround this with a try/catch block which is okay because I want to catch errors. Although, I have noticed I have never caught the IOException which I am forced to make a catch statement for.

So my question is, when would this error ever occur? I assumed it has to do with dealing with reading/writing to/from files, but in this case I am just reading in user input.

  • 2
    https://stackoverflow.com/questions/13216148/what-throws-an-ioexception-in-java Does this help? – Danyman Dec 07 '20 at 18:50

2 Answers2

2

Do not catch the exception just for the sake of formality

I will need to surround this with a try/catch block which is okay because I want to catch errors. Although, I have noticed I have never caught the IOException which I am forced to make a catch statement for.

If you are just catching the IOException and not doing anything meaningful to recover from it, it's better to remove the try-catch block and declare throws IOException in the method signature. This will ensure that the calling method will handle this in the required way.

If you are not doing anything meaningful to recover from the exception (in other words, you are not handling the exception) and catching it just because your IDE gave you the option to choose from try-catch or throws, do not catch it.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thanks. Also, when running the program I would redirect output by doing say java Driver < P1input.input >> P1Output.txt, would this throw a IOException is P1input.input is not found? As well, does adding the catch statement when not doing anything meaningful with it lower the programs speed? Thanks in advance –  Dec 07 '20 at 19:06
  • 1
    Thank you sir! :) –  Dec 07 '20 at 19:08
  • 2
    `does adding the catch statement when not doing anything meaningful with it lower the programs speed?` - No, but it will deny the opportunity for the calling method to handle the exception in the desired way. – Arvind Kumar Avinash Dec 07 '20 at 19:09
  • 1
    `when running the program I would redirect output by doing say java Driver < P1input.input >> P1Output.txt, would this throw a IOException is P1input.input is not found?` - Yes, indeed and that's the desired behaviour. Either you handle the exception (i.e. write logic to recover from the error or log the error message into the log file or console etc. and exit the application etc.) – Arvind Kumar Avinash Dec 07 '20 at 19:13
0

IOException is a really important and useful Exception in Java.

Think about loading a really large file, or reading something over sockets / over the network. If you get to the point where the file load is taking too long, you may want to cancel it. Programmatically, you can effect cancel by closing the stream. The thing is that when you do this, a thread may still waiting on receiving more data. So the read call (in your case readLine) will throw an IOException, which wakes up the thread that was waiting in read.

To cap things off, when you create a BufferedReader, it doesn't know about the underlying Reader/Stream. So to cover all cases, readLine needs to declare "throws IOException even if the underlying stream will never throw it.

Java is/was set up so that you must catch or throw non-RuntimeExceptions. This has its pluses and minuses, but it's just the way it is

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80