5. a. SyntaxErrorpython
b. IndexErrorcode
c. NameErrorutf-8
d. ZeroDivisionErrorinput
e. ValueErrorio
6. class
def safe_open(filename, mod='r'): try: fobj = open(filename, mod) except: fobj = None return fobj
7. 當語句 A 有異常或語句 A、B均無異常時,a、b執行結果一致無區別;當語句 A 無異常而語句 B 有異常時,a 執行完語句 A 後會拋出語句 B 的異常狀態,b 則會先執行語句 A、而後執行語句 except 。import
8.coding
# -*- coding:utf-8 -*- def safe_input(prompt): try: retval = raw_input(prompt) except (EOFError, KeyboardInterrupt): retval = None return retval
9.file
# -*- coding:utf-8 -*- import math def safe_sqrt(obj): try: return math.sqrt(obj) except ValueError: import cmath return cmath.sqrt(obj)