zoukankan      html  css  js  c++  java
  • python 异常类型

    1、NameError:尝试访问一个未申明的变量
    >>>  v
    NameError: name 'v' is not defined

    2、ZeroDivisionError:除数为0
    >>> v = 1/0
    ZeroDivisionError: int division or modulo by zero

    3、SyntaxError:语法错误
    >>> int int
    SyntaxError: invalid syntax (<pyshell#14>, line 1)

    4、IndexError:索引超出范围
    >>> List = [2]
    >>> List[3]
    Traceback (most recent call last):
      File "<pyshell#18>", line 1, in <module>
        List[3]
    IndexError: list index out of range

    5、KeyError:字典关键字不存在
    >>> Dic = {'1':'yes', '2':'no'}
    >>> Dic['3']
    Traceback (most recent call last):
      File "<pyshell#20>", line 1, in <module>
        Dic['3']
    KeyError: '3'

    6、IOError:输入输出错误
    >>> f = open('abc')
    IOError: [Errno 2] No such file or directory: 'abc'

    7、AttributeError:访问未知对象属性
    >>> class Worker:
     def Work():
      print("I am working")

    >>> w = Worker()
    >>> w.a
    Traceback (most recent call last):
      File "<pyshell#51>", line 1, in <module>
        w.a
    AttributeError: 'Worker' object has no attribute 'a'

    8、ValueError:数值错误
    >>> int('d')
    Traceback (most recent call last):
      File "<pyshell#54>", line 1, in <module>
        int('d')
    ValueError: invalid literal for int() with base 10: 'd'

    9、TypeError:类型错误
    >>> iStr = '22'
    >>> iVal = 22
    >>> obj = iStr + iVal;
    Traceback (most recent call last):
      File "<pyshell#68>", line 1, in <module>
        obj = iStr + iVal;
    TypeError: Can't convert 'int' object to str implicitly

    10、AssertionError:断言错误
    >>> assert 1 != 1
    Traceback (most recent call last):
      File "<pyshell#70>", line 1, in <module>
        assert 1 != 1
    AssertionError

    以上转自http://blog.csdn.net/fcoolx/article/details/4202872

    下面增加一些本人工作过程中遇到过的异常:

    11、MemoryError:内存耗尽异常

    12、NotImplementedError:方法没实现引起的异常

        示例:

    复制代码
    1 class Base(object):
    2     def __init__(self):
    3         pass
    4 
    5     def action(self):
    6         raise NotImplementedError
    复制代码

    定义一个类,一个接口方法action,如果直接调用action则抛NotImplementedError异常,这样做的目的通常是用来模拟接口

    13、LookupError:键、值不存在引发的异常

        LookupError异常是IndexError、KeyError的基类

       如果你不确定数据类型是字典还是列表时,可以用LookupError捕获此异常

    14、StandardError 标准异常。

        除StopIterationGeneratorExitKeyboardInterrupt 和SystemExit外,其他异常都是StandarError的子类。

      异常处理有别于错误检测:

    错误检测与异常处理区别在于:错误检测是在正常的程序流中,处理不可预见问题的代码,例如一个调用操作未能成功结束

     
     
  • 相关阅读:
    CRL线程池调度和配置的一些细节
    迁移到iis7
    musicstore edit方法出错的原因和解决方法
    如何分离出EF的三份结构定义文件
    在GridView中 鼠标移动到行 该行颜色变换
    飘逸程序员的老家
    [转贴]ASP.NET中常用的26个优化性能方案
    【转贴】在ASP.NET中显示进度条ASP.NET
    在使用GridView中删除的按钮弹出提示框最简单的一中方法
    【转贴】ASP.NET图表控件
  • 原文地址:https://www.cnblogs.com/testlife007/p/4195714.html
Copyright © 2011-2022 走看看