python学习(第八章)

异常

什么是异常

  • python用异常对象来表示异常情况。
  • 遇到错误后,会引发异常。如果异常对象并未被处理或捕捉,程序就会用所谓的回朔(traceback)终止执行。

按自己的方式出错

raise语句

  • 可以使用dir函数列出模块的内容:

    >>>import exceptions
    >>>dir(exceptions)
    ['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BufferError', 'BytesWarning', 'DeprecationWarning', 'EOFError', 'EnvironmentError', 'Exception', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '__doc__', '__name__', '__package__']
    
  • 内建异常

    类名 & 描述 \\
    Exception & 所有异常的基类 \\
    AttributeError & 特性引用或赋值失败时引发 \\
    IOERrror & 试图打开不存在文件时引发 \\
    IndexError & 在使用序列中不存在的索引时引发 \\
    KeyError & 在使用映射中不存在的键时引发 \\
    NameError & 在找不到名字时引发 \\
    SynatxError & 在代码为错误形式时引发 \\
    TypeError & 在内建操作或函数应用于错误类型的对象时引发 \\
    ValueError & 在内建操作或函数应用于正确类型的对象,但是该对象使用不适合的值时引发 \\
    ZeroDivisionError & 在除法或模除操作的第二个参数为0时引发 \\
    

自定义异常类

  • 编写一个自定义异常类:

    class SomeCustomException(Exception): pass
    

捕捉异常

  • 捕捉异常: 异常是可以处理的

    try:x = input('Enter the first number: ')y = input('Enter the second number: ')print x / yexcept ZeroDivisionError:print "The second number can't be zero!"

不止一个except子句

  • 为了捕捉这个异常,可以直接在同一个try/except语句后面加上另一个except子句:

    try:
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    print x / y
    except ZeroDivisionError:
    print "The second number can't be zero!"
    except TypeError:
    print "That wasn't a number. was it?"
    

用一个块捕捉多个异常

  • 如果需要用一个块捕捉多个类型异常,那么可以将它们作为元组列出:

    try:
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    print x / y
    except (ZeroDivisionError, TypeError, NameError):
    print 'Your numbers were bogus...'
    

捕捉对象

try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
print x / y
except (ZeroDivisionError, TypeError), e:
print e
  • [注: 在python3.0中,except子句会被写作except(ZeroDivision, TypeError) as e]

正真的全捕捉

  • 如果真的想用一段代码捕捉所有异常,那么可以在except子句中忽略所有的异常类:

    try:
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    print x / y
    except:
    print 'Something wrong happened...'
    

强悍的else

  • 有些情况中,一些坏事发生时执行一段代码是很有用的; 可以像对条件和循环语句那样,给try/except语句加个else子句:

    while True:
    try:
    x = input('Enter the first number: ')
    y = input('Enter the second number: ')
    value = x / y
    print 'x/y is', value
    except:
    print 'Invalid input. Please try again.'
    else:
    break
    

Finally...

  • Finally子句。它可以用来在可能的异常后进行清理。它和try子句联合使用:

    x = None
    try:
    x = 1 / 0
    except:
    print "can't be zero"
    finally:
    print 'Cleaing up...'
    del x
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值