题目
题目

Fal25_CS003C_74229 Ch. 7 Pre-Quiz (Remotely Proctored)

单项选择题

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?

选项
A.except RuntimeError :
B.except IndexError :
C.except EnvironmentError :
D.except IOError :
查看解析

查看解析

标准答案
Please login to view
思路分析
We need to fill in a except clause so that any exception raised during opening or reading the file is caught and the program prints "Error" instead of crashing. Option 1: except RuntimeError : This would only catch RuntimeError exceptions. File I/O operations typically raise OSError or its subclasses, not RuntimeError, so this block would not hand......Login to view full explanation

登录即可查看完整答案

我们收录了全球超50000道考试原题与详细解析,现在登录,立即获得答案。

更多留学生实用工具

加入我们,立即解锁 海量真题独家解析,让复习快人一步!