zoukankan      html  css  js  c++  java
  • python基础教程_学习笔记10:异常

    异常

    什么是异常

    Python用异常对象来表示异常情况。遇到错误后,会引发异常。假设异常对象并未被处理或捕捉,程序就会用所谓的回溯(Traceback,一种错误信息)终止运行:

    >>> 1/0

    Traceback (most recent call last):

      File "<pyshell#0>", line 1, in <module>

        1/0

    ZeroDivisionError: integer division or modulo by zero

    每一个异常都是一些类的实例,这些实例能够被引发,而且能够用非常多方法进行捕捉,使得程序能够捉住错误并对其进行处理,而不是让整个程序失败。

    按自己的方式出错

    异常能够在某些东西出错时自己主动引发

    raise语句

    为了引发异常,能够使用一个类(应该是Exception的子类)或者实例參数调用raise语句。使用类时,程序会自己主动创建实例。

    >>> raise Exception

    Traceback (most recent call last):

      File "<pyshell#1>", line 1, in <module>

        raise Exception

    Exception

    >>> raise Exception('hyperdrive overload')

    Traceback (most recent call last):

      File "<pyshell#2>", line 1, in <module>

        raise Exception('hyperdrive overload')

    Exception: hyperdrive overload

    第一个样例引发了一个没有不论什么有关错误信息的普通异常;

    第二个样例则加入了一些hyperdive overload错误信息。

    内建的异常类有非常多。

    >>> 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', 'WindowsError', 'ZeroDivisionError', '__doc__', '__name__', '__package__']

    全部这些异常都能够用于raise语句:

    >>> raise ZeroDivisionError

    Traceback (most recent call last):

      File "<pyshell#6>", line 1, in <module>

        raise ZeroDivisionError

    ZeroDivisionError

    >>> raise BaseException

    Traceback (most recent call last):

      File "<pyshell#7>", line 1, in <module>

        raise BaseException

    BaseException

    一些最重要的内建异常类

    类名 描写叙述
    Exception 全部异常的基类
    AttributeError 特性引用或赋值失败时引发
    IOError 试图打开不存在的文件(包含其它情况)时引发
    IndexError 在使用序列不存在的索引时引发
    KeyError 在使用映射中不存在的键时引发
    NameError 在找不到名字(变量)时引发
    SyntaxError 在代码为错误形式时引发
    TypeError 在内建操作或函数应用于错误类型的对象时引发
    ValueError 在内建操作或者函数应用于正确类型的对象,但该对象使用不合适的值时引发
    ZeroDivisionError 在除法或模除操作的第二个參数为0时引发

    自己定义异常类

    有些时候须要创建自己的异常类。

    class SomeCustomException(Exception): pass

    能够向自己主动义异常类中添加方法,此处什么也没做。

    捕捉异常

    关于异常最有意思的地方就是能够处理它们(通常叫做诱捕或者捕捉异常)。

    这个功能能够使用try/except来实现。

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    假设用户输入0作为第二个数,则出现ZeroDivisionError异常。

    为了捕捉异常而且做出一些错误处理:

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except ZeroDivisionError:

    print “The 2nd number can’t be zero!”

    看,没參数

    >>> class MuffledCalculator:

    muffled=False

    def calc(self,expr):

    try:

    return eval(expr)

    except ZeroDivisionError:

    if self.muffled:

    print "Division by zero is illegal"

    else:

    raise

    >>> calculator=MuffledCalculator()

    >>> calculator.calc('10/2')

    5

    >>> calculator.calc('10/0')

    Traceback (most recent call last):

      File "<pyshell#22>", line 1, in <module>

        calculator.calc('10/0')

      File "<pyshell#19>", line 5, in calc

        return eval(expr)

      File "<string>", line 1, in <module>

    ZeroDivisionError: integer division or modulo by zero

    >>> calculator.muffled=True

    >>> calculator.calc('10/0')

    Division by zero is illegal

    不止一个except子句

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except ZeroDivisionError:

    print “The 2nd number can’t be zero!”

    假设用户输入“HelloWorld”作为第二个參数,将引发错误:

    TypeError: unsupported operand type(s) for /: 'int' and 'str'

    由于except子句仅仅寻找ZeroDivisionError异常,这次的错误就溜过了检查并导致程序终止。为了捕捉这个异常,能够直接在同一个try/except语句后面加上还有一个except子句:

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except ZeroDivisionError:

    print “The 2nd number can’t be zero!”

    except TypeError:

    print “That wasn’t a number,was it?”

    用一个块捕捉两个异常

    假设须要用一个块捕捉多个类型异常,能够将它们作为元组列出:

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except (ZeroDivisionError,TypeError):

    print “Your numers are bogus!”

    捕捉对象

    假设想让程序继续执行,可是又由于某种原因想记录下错误,捕捉对象就非常实用。

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except (ZeroDivisionError,TypeError),e:

    print e

    真正的全捕捉

    就算程序能处理好几种类型的异常,但有些异常还是会从眼皮底下溜走。

    以除法为样例,在提示符下直接按回车,不输入不论什么东西,会得倒一个类似以下的错误信息:

    SyntaxError: unexpected EOF while parsing

    这个异常逃过了try/except语句的检查。这样的情况下,与其用那些并不是捕捉这些异常的try/except语句隐藏异常,还不如让程序立马崩溃。

    但假设真的想用一段代码捕获全部异常,能够在except子句中忽略全部的异常类:

    try:

    x=input(‘1st number: ’)

    y=input(‘2nd number: ’)

    print x/y

    except:

    print ‘Something wrong happened...’

    警告:像这样捕捉全部异常是危急的,由于它会隐藏全部程序猿未想到而且未做好准备处理的错误。它相同会捕捉用户终止运行的Ctrl+c企图,以及用sys.exit函数终止程序的企图,等等。这时用except Exception,e会更好些,或者对异常对象e进行一些检查。

    万事大吉

    有些情况下,一些坏事发生时运行一段代码是非常实用的,能够给try/except语句加个else子句:

    >>> try:

    print 'A simple task'

    except:

    print 'what?'

    else:

    print 'nothing'

    A simple task

    nothing

    最后......

    finally子句,用来在可能的异常后进行清理。

    >>> try:

    print 'A simple task'

    except:

    print 'what?'

    else:

    print 'nothing'

    finally:

    print 'clean up'

    A simple task

    nothing

    clean up

    异常和函数

    异常和函数非常自然地一起工作。假设异常在函数内引发而不被处理,它会传播至函数调用的地方。假设在那里也没有处理异常,它会继续传播,一直到达主程序(全局作用域)。假设那里没有异常处理程序,程序会带着堆栈跟踪终止。

    >>> def faulty():

    raise Exception('something is wrong')

    >>> def ignore_exception():

    faulty()

    >>> def handle_exception():

    try:

    faulty()

    except:

    print 'Exception handled'

    >>> ignore_exception()

    Traceback (most recent call last):

      File "<pyshell#58>", line 1, in <module>

        ignore_exception()

      File "<pyshell#51>", line 2, in ignore_exception

        faulty()

      File "<pyshell#48>", line 2, in faulty

        raise Exception('something is wrong')

    Exception: something is wrong

    >>> handle_exception()

    Exception handled

  • 相关阅读:
    页面渲染速度增加的方法和建议
    五(六)、Package & import
    五(五)、构造器 & JavaBean &this
    五(四)、封装性
    五(三)、方法
    五(二)、匿名对象
    五(一)、类&对象概述
    六、java 异常处理
    四、java 数组
    三、java 基础提炼
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/3924256.html
Copyright © 2011-2022 走看看