Python核心编程-第十章-习题

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)
相关文章
相关标签/搜索