• python基础教程学习笔记八


    第八章 异常

    Python中使用异常对象来表示异常情况

    >>> 1/0

    Traceback (most recent call last):

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

        1/0

    ZeroDivisionError: division by zero

    自定义异常 类似于java中的throws

    >>> raise Exception

    Traceback (most recent call last):

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

        raise Exception

    Exception

    >>> raise Exception('hyperdrive overload')

    Traceback (most recent call last):

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

        raiseException('hyperdrive overload')

    Exception: hyperdrive overload

    常见的异常类

    BaseException
    +--SystemExit
    +--KeyboardInterrupt
    +--GeneratorExit
    +--Exception
    +--StopIteration
    +--ArithmeticError
    |+--FloatingPointError
    |+--OverflowError
    |+--ZeroDivisionError
    +--AssertionError
    +--AttributeError
    +--BufferError
    +--EOFError
    +--ImportError
    +--LookupError
    |+--IndexError
    |+--KeyError
    +--MemoryError
    +--NameError
    |+--UnboundLocalError
    +--OSError
    |+--BlockingIOError
    |+--ChildProcessError
    |+--ConnectionError
    ||+--BrokenPipeError
    ||+--ConnectionAbortedError
    ||+--ConnectionRefusedError
    ||+--ConnectionResetError
    |+--FileExistsError
    |+--FileNotFoundError
    |+--InterruptedError
    |+--IsADirectoryError
    |+--NotADirectoryError
    |+--PermissionError
    |+--ProcessLookupError
    |+--TimeoutError
    +--ReferenceError
    +--RuntimeError
    |+--NotImplementedError
    +--SyntaxError
    |+--IndentationError
    |+--TabError
    +--SystemError
    +--TypeError
    +--ValueError
    |+--UnicodeError
    |+--UnicodeDecodeError
    |+--UnicodeEncodeError
    |+--UnicodeTranslateError
    +--Warning
    +--DeprecationWarning
    +--PendingDeprecationWarning
    +--RuntimeWarning
    +--SyntaxWarning
    +--UserWarning
    +--FutureWarning
    +--ImportWarning
    +--UnicodeWarning
    +--BytesWarning
    +--ResourceWarning

    自定义异常

    >>> class SomeCustomException(Exception):pass

    >>> raise SomeCustomException

    Traceback (most recent call last):

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

        raise SomeCustomException

    SomeCustomException

    捕捉异常

    #未捕捉

    x=input('enter the first number:')

    y=input('enter the second number:')

    z=eval(x)/eval(y)

    print (z)

    enter the first number:2

    enter the second number:1

    2.0

    enter the first number:1

    enter the second number:0

    Traceback (most recent call last):

      File"D:/workspace_python/exception.py", line 3, in <module>

        z=eval(x)/eval(y)

    ZeroDivisionError: division by zero

    #捕捉

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except ZeroDivisionError:

        print('The second numbercan not be zero')

    enter the first number:1

    enter the second number:0

    The second number can not be zero

    多个except子句

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except ZeroDivisionError:

    print('The second number can not be zero')

    except TypeError:

        print('that was a number,was it?')

    一个块捕捉多个异常

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except (ZeroDivisionError,TypeError):

    print('your numbers were bogus')

    捕捉对象:

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except ZeroDivisionError as e:

        print('The second numbercan not be zero')

        print(e)

    enter the first number:1

    enter the second number:0

    The second number can not be zero

    division by zero

    全捕捉

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except:

            print('someThing wronghappended')

    enter the first number:1

    enter the second number:0

    someThing wrong happended

    Filally子句

    try:

        x=input('enter the firstnumber:')

        y=input('enter the secondnumber:')

        z=eval(x)/eval(y)

        print (z)

    except:

        print('someThing wronghappended')

    finally:

        print('clenaing up...')

    enter the first number:1

    enter the second number:0

    someThing wrong happended

    clenaing up...

    向上传播:

    可以使用raise抛出异常

  • 相关阅读:
    (转)创建DB2实例时出错,请大家帮忙解决
    lscons 命令,设置当前控制台设备的名称写至标准输出
    (转)AIX下修改用户最大进程数
    (转)AIX 5.3 安装中文语言包
    (转)AIX修改系统时区的3种方法和AIX 时间问题(夏令时)
    (转)企业级NFS网络文件共享服务
    一天一个mysql函数(二) FIND_IN_SET()
    sql语句备忘
    一天一个mysql函数(一) cast && convert
    c语言海量数据处理
  • 原文地址:https://www.cnblogs.com/retacn-yue/p/6194203.html
Copyright © 2020-2023  润新知