zoukankan      html  css  js  c++  java
  • 异常处理

    异常处理

    python中常见的异常种类

    复制代码
    AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x
    IOError 输入/输出异常;基本上是无法打开文件
    ImportError 无法引入模块或包;基本上是路径问题或名称错误
    IndentationError 语法错误(的子类) ;代码没有正确对齐
    IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5]
    KeyError 试图访问字典里不存在的键
    KeyboardInterrupt Ctrl+C被按下
    NameError 使用一个还未被赋予对象的变量
    SyntaxError Python代码非法,代码不能编译(个人认为这是语法错误,写错了)
    TypeError 传入对象类型与要求的不符合
    UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量
    导致你以为正在访问它
    ValueError 传入一个调用者不期望的值,即使值的类型是正确的
    复制代码

    更多异常

    复制代码
    ArithmeticError
    AssertionError
    AttributeError
    BaseException
    BufferError
    BytesWarning
    DeprecationWarning
    EnvironmentError
    EOFError
    Exception
    FloatingPointError
    FutureWarning
    GeneratorExit
    ImportError
    ImportWarning
    IndentationError
    IndexError
    IOError
    KeyboardInterrupt
    KeyError
    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
    ArithmeticError
    AssertionError
    AttributeError
    BaseException
    BufferError
    BytesWarning
    DeprecationWarning
    EnvironmentError
    EOFError
    Exception
    FloatingPointError
    FutureWarning
    GeneratorExit
    ImportError
    ImportWarning
    IndentationError
    IndexError
    IOError
    KeyboardInterrupt
    KeyError
    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

    什么是异常处理

    python解释器检测到错误,触发异常(也允许程序员自己触发异常)

    程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,与异常处理有关)

    如果捕捉成功则进入另外一个处理分支,执行你为其定制的逻辑,使程序不会崩溃,这就是异常处理

    try except语句

    需要检测异常的代码放在try代码块
    需要处理的代码放在except代码块
    不报错不执行except内的代码,except处理的错误名称应该和实际报错一致,否则无效
    如果报错,try中报错之后的代码不执行
    不影响try外面的代码
    except ErrorName as 变量名 ,变量名中存储的就是错误的具体提示
    except支持多分支处理的方式,从上到下先知道到能处理的error类型就执行该except中的内容
    万能异常 except Exception as e ,e为错误信息提示,应该放在所有except之后
    对于你已经预料的错误,应该使用指定的异常进行特殊处理
    万能异常是用来处理预料不到的异常的

    复制代码
    try:
        num = int(input('请输入序号:'))
        print(num)
    except NameError as e:
        print('NameError 您输入的内容不和法', e)
    except ValueError as e:
        print('ValueError 您输入的内容不和法',e)
    except Exception as e:
        print('出错了',e)
    
    try:
        num = int(input('请输入序号:'))
    except Exception as e:
        print('出错了',e)
    else:  # 如果try中的代码不会发生异常,就走这个else中的内容
        print('没毛病')
    finally:  # 不管异不异常都走这个
        print('*********')
    
    def func():
        f = open('f','w')
        try:
            for i in range(10):
                f.write(i)
        except Exception:
            print('出错了')
            return
        finally:  # 在一个函数中操作一个文件 需要关闭 在finally中关闭
            f.close()
    复制代码

    异常处理:不要在大段代码外面加*******************

  • 相关阅读:
    Android文字上下滚动
    Java怎么去除字符串中的多个空白【split(" ")与split("\s+")】
    Java:Scanner.nextLine()和Scnner.next()的区别,以及多条nextLine()输入问题的解决方法
    SQL server报错18456(又名SQL server怎么用sa登入)
    Android Studio快速自动生成findViewById
    java.sql.Date和java.util.Date的联系与区别【转载】
    Linux Crontab 不执行
    PostgreSQL 全量 增量 归档 备份工具 pg_rman介绍与使用(转载)
    Rman备份的保留策略(retention policy) (转载)
    Oracle RMAN block_change_tracking(块更改追踪)
  • 原文地址:https://www.cnblogs.com/QQ279366/p/7840002.html
Copyright © 2011-2022 走看看