面试复习-Python-错误与异常

2024-10-09 21:08:09 浏览数 (1)

Python中常见的异常

代码语言:javascript复制
>>> for
  File "<stdin>", line 1
    for
       ^
SyntaxError: invalid syntax
>>> name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
>>> 5/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>> f[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '_io.TextIOWrapper' object is not subscriptable
>>> l = [1]
>>> l[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> d = {'name': 'Sen'}
>>> d.name
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'name'
>>> d['name']
'Sen'
>>> f = open("exec.txt")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'exec.txt'

Python的异常处理

代码语言:javascript复制
>>> try:
...     f = open("ex", 'r')
... except IOError as e:
...     print(e)
...
[Errno 2] No such file or directory: 'ex'
代码语言:javascript复制
# 捕获所有异常
>>> try:
...     f = open("ex", 'r')
... except IOError as e:
...     print(e)
...     raise
... except Exception as e:
...     print(e)
...
[Errno 2] No such file or directory: 'ex'
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'ex'
代码语言:javascript复制
# 忽略所有异常
>>> try:
...     f = open("ex", 'r')
... except Exception:
...     pass
代码语言:javascript复制
>>> try:
...     f = open('a.txt', 'r')
... except IOError:
...     print('file not found')
... else:
...     print('file opened')
... finally:
...     print('file handled')
...
file opened
file handled

上下文管理协议

代码语言:javascript复制
>>> with open('a.txt', 'r') as f:
...     f.readlines()
...
['foon', 'barn']

0 人点赞