185k views
5 votes
Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline() print(line) _____________________ print("Error") What should be placed in the blank so that the program will print Error instead of crashing if an exception occurs while opening or reading from the file?

User Georgi
by
5.5k points

1 Answer

2 votes

Answer:

The answer is "except IOError:"

Explanation:

It is part of the exception handling, that stands for input/ output operation fails, this exception aeries when we attempting to access an anti-existent file, in the program so, it will give the program related errors.

  • This type of error is handled in the try block, it also allows you to prepare the deal with the exceptional block.
  • It also helps you to hide the bugs by using code, if you handle an unexpected error.
User Jochen Holzer
by
4.5k points