zoukankan      html  css  js  c++  java
  • Python中的异常类型

    http://blog.csdn.net/fcoolx/article/details/4202872

    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

  • 相关阅读:
    2020-04-07 python一行代码 http服务器文件共享
    2020-04-06 linux命令之awk
    2020-04-05 ubuntu安装docker并使用国内加速
    2020-04-04 ssh免密登录
    尚学堂 JAVA DAY11 概念总结
    尚学堂 JAVA Day3 概念总结
    尚学堂 JAVA Day1 概念总结
    Android Studio 首次安装报错 Java.lang.RuntimeException:java.lang.NullPointerException...错
    Android 迷之Version管理
    Android Develop 之 Ddevelop WorkFlow Basics
  • 原文地址:https://www.cnblogs.com/lvxiuquan/p/2998126.html
Copyright © 2011-2022 走看看