#!/usr/bin/env python
# -*- coding: utf-8-*-# Author: Mason
try:
names =["Huajuan","NianGao"]
names_dict ={
"First":"Huajuan","Second":"Niangao"}# IndexError: list index out of range
names[2]# KeyError: 'Third'
names_dict["Third"]
except IndexError as index_error:print("数据下标越界:%s"% index_error)
except KeyError as key_error:print("字典中没有key: %s"% key_error)
except Exception as exception:print("Exception: %s"% exception)
except BaseException as base_exception:print("BaseException: %s"% base_exception)
# 输出:
数据下标越界:list index out of range
2、一个except子句可以将多个异常命名为带括号的元组
示例: except (RuntimeError, TypeError, NameError) as e: pass
#!/usr/bin/env python
# -*- coding: utf-8-*-# Author: Mason
try:
names =["Huajuan","NianGao"]
names_dict ={
"First":"Huajuan","Second":"Niangao"}# IndexError: list index out of range
names[2]# KeyError: 'Third'
names_dict[