Java Java Data Structures Efficiency! Design the UI

Why does it throw an exception?

In the video, Craig say, String choice = mReader.readLine(); throws an exception, "IOException". My question is why does it throw an IOException?

1 Answer

Tonnie Fanadez
Tonnie Fanadez
UX Design Techdegree Graduate 22,154 Points

Hello there.

A file system read can fail for many reasons at any time e.g. the file is deleted or network errors or other OS errors.

An exception is thrown because you can't know or predict what will happen with code in case of the problems noted above. Therefore the programmer should think ahead and take care of any occurrences by catching the exceptions on try-catch block.