#coding=utf-8
try:
get_mutex()
do_some_stuff()
except (IndexError,KeyError,AttributeError),e:
log("ERROR:data retrieval accessing a non-existent element")
finally:
free_mutex()
# 用raise抛出异常
'''
加入你在自己的库里创建了一个api调用要求传入一个大于0的正整数。在内置
函数isinstance的帮助下(检查对象类型),你代码看起来应该是这样的:
'''
def foo(must_be_positive_int):
''' foo() --take positive integer and process it '''
#check if integer
if not isinstace(must_be_positive_int):
raise TypeError("ERROR foo():must pass in an integer")
#check if positive
if must_be_positive_int<1:
raise ValueError("ERROR foo():integer must be greater than zerio!")
#normal processing here