异常处理
例一
>>> i = input(‘请输入数字‘)
请输入数字:0
>>> print(i)
0
>>> print(5 / int(i))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
上述代码的报错是除零的错误,数学上不允许除零
例二
>>> i="QWE"
>>> print(5 / int(i))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ‘QWE‘
i是字符串类型,而且是QWE,不能转换成int类型,所以抛出异常
Python中的异常根类是BaseException,结构图如下
BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StandardError
      |    +-- BufferError
      |    +-- ArithmeticError
      |    |    +-- FloatingPointError
      |    |    +-- OverflowError
      |    |    +-- ZeroDivisionError
      |    +-- AssertionError
      |    +-- AttributeError
      |    +-- EnvironmentError
      |    |    +-- IOError
      |    |    +-- OSError
      |    |         +-- WindowsError (Windows)
      |    |         +-- VMSError (VMS)
      |    +-- EOFError
      |    +-- ImportError
      |    +-- LookupError
      |    |    +-- IndexError
      |    |    +-- KeyError
      |    +-- MemoryError
      |    +-- NameError
      |    |    +-- UnboundLocalError
      |    +-- ReferenceError
      |    +-- RuntimeError
      |    |    +-- NotImplementedError
      |    +-- SyntaxError
      |    |    +-- IndentationError
      |    |         +-- TabError
      |    +-- SystemError
      |    +-- TypeError
      |    +-- ValueError
      |         +-- UnicodeError
      |              +-- UnicodeDecodeError
      |              +-- UnicodeEncodeError
      |              +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
	   +-- ImportWarning
	   +-- UnicodeWarning
	   +-- BytesWarning
访问不存在的成员
例如
>>> class Animal(object):
	pass
>>> a1 = Animal()
>>> a1.run()
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    a1.run()
AttributeError: ‘Animal‘ object has no attribute ‘run‘
>>> 
>>> print(a1.age)
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    print(a1.age)
AttributeError: ‘Animal‘ object has no attribute ‘age‘
程序中创建了一个类叫Animal,但是并没有run函数和age变量,所以抛出异常
OSError是操作系统相关异常,例如试图打开一个不存在的文件
>>> f=open("abc.txt")
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    f=open("abc.txt")
FileNotFoundError: [Errno 2] No such file or directory: ‘abc.txt‘
是访问元素时,下标超出索引最大值或最小值引发异常例如:
>>> code_list = [125, 56, 89, 36]
>>> code_list[4]
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    code_list[4]
IndexError: list index out of range
是试图访问字典里不存在的键引发,例如:
>>> dict1[104]
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    dict1[104]
NameError: name ‘dict1‘ is not defined
104在字典中不存在,O(∩_∩)O哈哈~那就报错吧
NameError是指试图使用一个不存在的变量引发的异常,PythonShell中运行实例
>>> value1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name ‘value1‘ is not defined
>>> a = value1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name ‘value1‘ is not defined
>>> value1 = 10
>>> value1
10
赋值和直接取值都不行,因为没有这个value1变量
只有最后的value1 = 10,才让value1有了值
TypeError是试图传入变量类型与要求的不符合时而引发的异常。PythonShell运行实例、
>>> i = ‘2‘
>>> print(5 / i)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    print(5 / i)
TypeError: unsupported operand type(s) for /: ‘int‘ and ‘str‘
i是一个字符类型,而5是整形,不能进行除法运算,因为类型不统一
ValueError是由于传入一个无效的参数值而引发的异常,这个异常在前面已经遇到了
>>> del i
>>> i = ‘QWE‘
>>> print( 5 / int(i))
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print( 5 / int(i))
ValueError: invalid literal for int() with base 10: ‘QWE‘
我不会捕获异常,不会使用try和except等等,看也看不懂啊……??
原文:https://www.cnblogs.com/coding365/p/12685761.html