Python-异常处理

199 阅读1分钟
sep='_'*32+'\n'
print(sep+'EXCRPTION RAISED AND CAUGHT')
try:
	x='spam'[99]
except IndexError:
	print('ecept run')
finally:
	print('finally run')
print('after run')

print(sep+'NO EXCRPTION RAISED AND CAUGHT')
try:
	x='spam'[3]
except IndexError:
	print('ecept run')
finally:
	print('finally run')
print('after run')

print(sep+'NO EXCRPTION RAISED AND CAUGHT withelse')
try:
	x='spam'[3]
except IndexError:
	print('ecept run')
else:
	print('else run')
finally:
	print('finally run')
print('after run')

print(sep+'EXCRPTION RAISED NOT CAUGHT')
try:
	x=1/0
except IndexError:
	print('ecept run')
finally:
	print('finally run')
print('after run')


________________________________
EXCRPTION RAISED AND CAUGHT
ecept run
finally run
after run
________________________________
NO EXCRPTION RAISED AND CAUGHT
finally run
after run
________________________________
NO EXCRPTION RAISED AND CAUGHT withelse
else run
finally run
after run
________________________________
EXCRPTION RAISED NOT CAUGHT
finally run
Traceback (most recent call last):
  File "mergedexc.py", line 33, in <module>
    x=1/0
ZeroDivisionError: division by zero


------------------
(program exited with code: 1)

请按任意键继续. . .