zoukankan      html  css  js  c++  java
  • 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抛出异常

  • 相关阅读:
    ASP.NET之Application、Session和Cookie的差别
    Android 最火高速开发框架AndroidAnnotations使用具体解释
    关于牛逼的顺丰--也谈管理
    SIFT特征提取分析
    [置顶] 网页提交方式post和get的区别和联系
    xcode 5.0 以上去掉icon高亮方法&amp;iOS5白图标问题
    我是怎样成长为系统架构师的
    怎样使用oracle 的DBMS_SQLTUNE package 来执行 Sql Tuning Advisor 进行sql 自己主动调优
    UDP用户数据报协议和IP分组
    使用Java高速实现进度条
  • 原文地址:https://www.cnblogs.com/retacn-yue/p/6194203.html
Copyright © 2011-2022 走看看