# coding=utf-8_
_author__ = "leaves"
# raise 可以引发异常
# raise Exception
# 捕获异常
try:
x = '10'
y = 0
print x / y
except ZeroDivisionError:
print "The second number can't be zero "
except TypeError:
print "That wasn't a number .was it"
try:
print 'A simple task'
except:
print 'What? Something went wrong'
else:
print 'Ah...It went as planned'
finally:
print 'I am the Last'
_author__ = "leaves"
# raise 可以引发异常
# raise Exception
# 捕获异常
try:
x = '10'
y = 0
print x / y
except ZeroDivisionError:
print "The second number can't be zero "
except TypeError:
print "That wasn't a number .was it"
try:
print 'A simple task'
except:
print 'What? Something went wrong'
else:
print 'Ah...It went as planned'
finally:
print 'I am the Last'
本文深入探讨了Python中异常处理机制的使用方法,通过实例展示了如何捕获和处理不同类型的异常,包括ZeroDivisionError和TypeError。同时,介绍了通用异常处理方式及else和finally语句的应用,为Python开发者提供了实用的编程技巧。
147

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



