Java: what are IOEXceptions in BufferedReader's readLine() for?

后端 未结 6 2180
说谎
说谎 2021-02-06 03:13

I can \"fix\" the below exception with a try-catch loop but I cannot understand the reason.

  1. Why does the part \"in.readLine()\" continuosly ignite IOExceptions? <
6条回答
  •  忘了有多久
    2021-02-06 03:59

    It is thrown if an exceptional situation occurs with the I/O, for example the source of the stream is no longer available.

    In such cases your program should be able to recover. Either by re-reading the source, or by using some defaults, or by alerting the user about the problem.

    You are forced to catch it, because it is a checked exception, and you are supposed to be able to recover from those.

    Of course, you have the option to declare that the current menthod throws this exception to caller methods, but you will have to catch it eventually (or let it bubble up to the main method, when it is simply printed on the console and the program execution stops)

提交回复
热议问题