try:
except Exception:
例子:
class Dog(object):
def __init__(self, name):
self.name = name
def eat(self, food):
print("%s is eating... %s" %(self.name, food))
d = Dog('liuhuayang')
try:
print(getattr(d, "age"))
except Exception:
print("extions")
例子2:
data = {"name":"liaodalin"}
try:
age = data["age"]
except KeyError:
print("there is no key for age") # 打印
# 给键制作
data = {"name":"liaodalin"}
try:
age = data["age"]
except KeyError as e:
print("there is no key for age ", e)
else的作用:
try:
xxx
except Exception:
print("有错")
else:
print("一切正常!")
finally:
pring("不管有没有错,都执行")