13.异常处理

一、判断题(共10小题,10分)
题型得分 9
内置异常类的父类是Exception。
【来源】
《Python程序设计基础》第9章思考与练习。

(1分)
A. 对
B. 错
我的答案:
B
题目得分 1
参考答案:
B
异常处理将错误检测和错误处理分隔开来,使程序复杂化。程序中异常处理结构在绝大多数情况下是没必要的。

(1分)
A. 对
B. 错
我的答案:
B
题目得分 1
参考答案:
B
不论是否发生异常,finally语句中的代码总是会执行的。
【来源】
《Python程序设计基础》第9章思考与练习。(1分)
A. 对
B. 错
我的答案:
A
题目得分 1
参考答案:
A
异常是程序运行时错误。严格说来,语法错误和逻辑错误不属于异常。
(1分)
A. 对
B. 错
我的答案:
B
题目得分 0
参考答案:
A
异常处理结构也不是万能的,处理异常的代码也有引发异常的可能。(1分)
A. 对
B. 错
我的答案:
A
题目得分 1
参考答案:
A
try语句可以有一个或多个finally语句。
【来源】
《Python程序设计基础》第9章思考与练习。(1分)
A. 对
B. 错
我的答案:
B
题目得分 1
参考答案:
B
通常情况下,用户自定义异常类都应该派生自Exception类。(1分)
A. 对
B. 错
我的答案:
A
题目得分 1
参考答案:
A
断言语句assert通常在程序调试阶段使用,辅助调试程序。(1分)
A. 对
B. 错
我的答案:
A
题目得分 1
参考答案:
A
若try语句中的代码引发了异常,则会执行else语句中的代码。
【来源】
《Python程序设计基础》第9章思考与练习。(1分)
A. 对
B. 错
我的答案:
B
题目得分 1
参考答案:
B
try语句可以有一个或多个except语句。
【来源】
《Python程序设计基础》第9章思考与练习。(1分)
A. 对
B. 错
我的答案:
A
题目得分 1
参考答案:
A
二、填空题(共15小题,30分)
题型得分 22
若输入10,下面程序的输出结果为________。

try:
temperature= eval(input("Enter a temperature: "))
if temperature > 40:
raise RuntimeError(“It is too hot”)
if temperature < 0:
raise RuntimeError(“It is too cold”)
except RuntimeError as ex:
print(ex)
except:
print(“Other errors”)
else:
print(temperature)

(2分)
我的答案:
Enter a temperature: Other errors
题目得分 0
参考答案:
10
下面程序的输出结果为________ (注意:不要有任何多余的空格)。

def main():
try:
value = 30
if value < 40:
raise Exception(“value is too small”)
except Exception as ex:
print(ex)

main()
(2分)
我的答案:
value is too small
题目得分 2
参考答案:
value is too small
若输入-10,下面程序的输出结果为________ (注意:不要有任何多余的空格)。

try:
temperature= eval(input("Enter a temperature: "))
if temperature > 40:
raise RuntimeError(“It is too hot”)
if temperature < 0:
raise RuntimeError(“It is too cold”)
except RuntimeError as ex:
print(ex)
except:
print(“Other errors”)
else:
print(temperature)

(2分)
我的答案:
Enter a temperature: It is too cold
题目得分 0
参考答案:
It is too cold
若输入50,下面程序的输出结果为________ (注意:不要有任何多余的空格)。

try:
temperature= eval(input("Enter a temperature: "))
if temperature > 40:
raise RuntimeError(“It is too hot”)
if temperature < 0:
raise RuntimeError(“It is too cold”)
except RuntimeError as ex:
print(ex)
except:
print(“Other errors”)
else:
print(temperature)

(2分)
我的答案:
Enter a temperature: It is too hot
题目得分 0
参考答案:
It is too hot
假设list1= [‘a’, ‘b’, ‘c’],执行list1[3],将抛出________异常。
【来源】
《Python程序设计基础》第9章思考与练习。(2分)
我的答案:
IndexError
题目得分 2
参考答案:
IndexError
使用math.sqrt(x)数学函数求x的平方根,若x为负数,将产生________异常。(2分)
我的答案:
ValueError
题目得分 2
参考答案:
ValueError
假设dict1= {0: ‘female’, 1: ‘male’},执行dict1[2],将抛出________异常。
【来源】
《Python程序设计基础》第9章思考与练习。(2分)
我的答案:
KeyError
题目得分 2
参考答案:
KeyError
Python内置异常类的父(基)类是________(注:填写英文)。

(2分)
我的答案:
BaseException
题目得分 2
参考答案:
BaseException
若输入t,下面程序的输出结果为________ (注意:不要有任何多余的空格)。

try:
temperature= eval(input("Enter a temperature: "))
if temperature > 40:
raise RuntimeError(“It is too hot”)
if temperature < 0:
raise RuntimeError(“It is too cold”)
except RuntimeError as ex:
print(ex)
except:
print(“Other errors”)
else:
print(temperature)

(2分)
我的答案:
Enter a temperature: Other errors
题目得分 0
参考答案:
Other errors
假设下面try-except块中的statement2语句出现一个异常:
try:
statement1
statement2
statement3
except Exception1:
# Handle exception 1
except Exception2:
# Handle exception 2
except Exception3:
# Handle exception 3
finally:
statement4
statement5
如果异常未被捕获,statement5会被执行吗?________ (注意:只能填写"是"或"否")。

(2分)
我的答案:

题目得分 2
参考答案:

假设下面try-except块中的statement2语句出现一个异常:
try:
statement1
statement2
statement3
except Exception1:
# Handle exception 1
except Exception2:
# Handle exception 2
statement4
如果异常被捕获,statement4会被执行吗?________ (注意:只能填写"是"或"否")。

(2分)
我的答案:

题目得分 2
参考答案:

对于下面try-except块:
try:
# some code here
except ArithmeticError:
print(“ArithmeticError”)
except ZeroDivisionError:
print(“ZeroDivisionError”)
这样的写法是否正确?________ (注意:只能填写"是"或"否")。
(2分)
我的答案:

题目得分 2
参考答案:

执行123 + “abc”,将抛出________异常。
【来源】
《Python程序设计基础》第9章思考与练习。(2分)
我的答案:
TypeError
题目得分 2
参考答案:
TypeError
假设下面try-except块中的statement2语句出现一个异常:
try:
statement1
statement2
statement3
except Exception1:
# Handle exception 1
except Exception2:
# Handle exception 2
statement4
statement3会被执行吗?________ (注意:只能填写"是"或"否")。(2分)
我的答案:

题目得分 2
参考答案:

假设下面try-except块中的statement2语句出现一个异常:
try:
statement1
statement2
statement3
except Exception1:
# Handle exception 1
except Exception2:
# Handle exception 2
except Exception3:
# Handle exception 3
finally:
statement4
statement5
如果异常类型是Exception3,那么statement4和statement5会被执行吗?________ (注意:只能填写"是"或"否")。(2分)
我的答案:

题目得分 2
参考答案:

三、单项选择题(共10小题,20分)
题型得分 20
下列程序执行时,将发生( )异常。

my_result = a * 8

(2分)
A. ValueError
B. NameError
C. SyntaxError
D. ZeroDivisionError
E. OverflowError
我的答案:
B
题目得分 2
参考答案:
B
下列程序执行时,将发生( )异常。

a = 12
b= 12 - 24
my_result= a / (a + b)

(2分)
A. ValueError
B. NameError
C. SyntaxError
D. ZeroDivisionError
E. OverflowError
我的答案:
D
题目得分 2
参考答案:
D
下列程序执行时,将发生( )异常。

a = 12
b = 13
my_result = (a + b / 2a)

(2分)
A. ValueError
B. NameError
C. SyntaxError
D. ZeroDivisionError
E. OverflowError
我的答案:
C
题目得分 2
参考答案:
C
下列程序执行时,将发生( )异常。

a = 30
b = 74353634.89
my_result= a ** b

(2分)
A. ValueError
B. NameError
C. SyntaxError
D. ZeroDivisionError
E. OverflowError
我的答案:
E
题目得分 2
参考答案:
E
下列程序执行时,将发生( )异常。

int(“3.4”)

(2分)
A. ValueError
B. NameError
C. SyntaxError
D. ZeroDivisionError
E. OverflowError
我的答案:
A
题目得分 2
参考答案:
A
下列程序的输出结果为( )。

try:
list = 5 * [0]
x = list[5]
print(“Done”)
except IndexError:
print(“Index out of bound”)

(2分)
A. 先输出"Done",接着输出"Index out of bound"
B. “Index out of bound”
C. “Done”
D. 没有输出
我的答案:
B
题目得分 2
参考答案:
B
下列程序的输出结果为( )。

try:
list = 10 * [0]
x = list[9]
print(“Done”)
except IndexError:
print(“Index out of bound”)
else:
print(“Nothing is wrong”)
finally:
print(“Finally we are here”)

(2分)
A. 先输出"Done",接着输出"Nothing is wrong"
B. 先输出"Done",接着输出"Nothing is wrong",最后输出"Finally we are here"
C. 先输出"Index out of bound",接着输出"Nothing is wrong",最后输出"Finally we are here"
D. 先输出"Nothing is wrong",接着输出"Finally we are here"
我的答案:
B
题目得分 2
参考答案:
B
下列程序的输出结果为( )。

def main():
try:
f()
print(“After the function call”)
except IndexError:
print(“Index out of bound”)
except:
print(“Exception in main”)

def f():
try:
s = “abc”
print(s[3])
except ZeroDivisionError:
print(“Divided by zero!”)

main()

(2分)
A. 先输出"Divided by zero!",接着输出"After the function call"
B. “After the functioncall”
C. “Index out of bound”
D. 先输出"Divided by zero!",接着输出"Exception in main"
我的答案:
C
题目得分 2
参考答案:
C
下列程序的输出结果为( )。

def main():
try:
f()
print(“After the functioncall”)
except ZeroDivisionError:
print(“Divided by zero!”)
except:
print(“Exception”)

def f():
print(1 / 0)

main()

(2分)
A. 先输出"After the function call",接着输出"Divided by zero!"
B. “After the function call”
C. “Divided by zero!”
D. 先输出"Divided by zero!",接着输出"Exception"
我的答案:
C
题目得分 2
参考答案:
C
下列程序的输出结果为( )。

try:
list = 10 * [0]
x = list[10]
print(“Done”)
except IndexError:
print(“Index out of bound”)
else:
print(“Nothing is wrong”)
finally:
print(“Finally we are here”)

(2分)
A. 先输出"Done",接着输出"Nothing is wrong"
B. 先输出"Done",接着输出"Nothing is wrong",最后输出"Finally we are here"
C. 先输出"Index out of bound",接着输出"Nothing is wrong",最后输出"Finally we are here"
D. 先输出"Nothing is wrong",接着输出"Finally we are here"
E. 先输出"Index out of bound",接着输出"Finally we are here"
我的答案:
E
题目得分 2
参考答案:
E

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值