异常
异常产生,检测到错误且解释器认为是异常,抛出异常;异常处理,截获异常,忽略或终止程序处理异常。

首先给出两个实验txt文件,内容如下:

| try…except… |
捕获指定类型的异常
try:
a
except NameError as e: #捕获NameError类的异常
print('catch Error:',e)
print('exec over')

b=0
try:
b
except NameError as e:
print('catch Error:',e)
print('exec over')

捕获所有类型的异常
try:
undefvar #之前没定义过的变量
except: #捕获所有异常
print('catch error')
#except Exception as e:
#Exception可以将所有的异常包括在内;将异常赋予变量e

指定捕获异常类型与实际触发异常类型不同
try:
undefvar #之前没定义过的变量
except FileNotFoundError as e: #捕获FileNotFoundError异常
print('catch error',e)

try:
open('test1.txt') #打开之前不存在文件
except FileNotFoundError as e: #捕获FileNotFoundError异常
print('catch error',e)

捕获多种异常类型
try:
f=open('one.txt')
line=f.read(2) #读取文件one.txt里的前两个字符
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)

| try…except…else… |
try语句捕获到异常执行except语句
try语句没有捕获到异常执行else语句
try:
f=open('changeone.txt')
line=f.read(2) #读取文件changeone.txt里的前两个字符
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
else:
print('No Error')

try:
f=open('changeone1.txt')
line=f.read(2) #读取文件changeone.txt里的前两个字符
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
else:
print('No Error')

| try…finally… |
如果try没有捕获到异常,先执行finally后的代码块;
如果try捕获到异常,先执行finally后的代码块,由python解释器处理捕获到的异常
try-finally语句:无论是否检测到异常,都会执行finally代码;不是为解释异常而设计,主要是做一些清理工作,关闭文件或释放系统资源
try:
f=open('one.txt')
print(int(f.read()))
finally:
print('close file')
f.close()

| 组合 |
try-except-finally
若try没有捕获到异常,执行完try代码块后,执行finally
若try捕获到异常,首先执行except处理错误,然后执行finally
try:
ft=open('changeone.txt')
line=ft.read(2) #读取文件changeone.txt里的前两个字符
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
finally:
print('close file')
ft.close()

try:
ff=open('changeone1.txt') #open时发生错误,f变量定义失败
line=ff.read(2)
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
finally:
print('close file')
ff.close()

try:
ff=open('changeone1.txt') #open时发生错误,f变量定义失败
line=ff.read(2)
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
finally:
try:
print('close file')
ff.close()
except NameError as e: #捕获变量ff的错误
print('NameError:,',e)

try-except-else-finally
若try没有捕获到异常,执行完try代码段后,执行else代码段,最后执行finally
若try捕获到异常,首先执行except处理错误,然后执行finally
try:
feef=open('changeone.txt') #open时正常读入,没发生错误
line=feef.read(2)
numd=int(line)
print('read num=%d' % numd)
except FileNotFoundError as e:
print('catct FileNotFoundError',e)
except ValueError as e:
print('ValueError',e)
else:
print('No Error')
finally:
try:
print('close file')
feef.close()
except NameError as e:
print('NameError:,',e)

| with语句 |
with语句,作用等同于try-except-finally语句的
import os
try:
with open('one.txt') as fw:
print('in with fw.read:',fw.read())
fw.seek(-5,os.SEEK_SET) #出现异常
except ValueError as e:
print('in with catch ValueError:',e)
print('with:',fw.closed)


本文详细介绍了Python中异常处理的方法,包括捕获指定类型的异常、捕获所有类型的异常、使用try...except...else...及try...finally...结构进行异常处理,并通过多个实例展示了如何正确地使用这些结构。

711

被折叠的 条评论
为什么被折叠?



