https://docs.python.org/3/library/exceptions.html#exception-hierarchy
Exception hierarchy
The class hierarchy for built-in exceptions is:
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- 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
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning
python常见的报错提示
在运行或编写一个程序时常会遇到错误异常,这时python会给你一个错误提示类名,告诉出现了什么样的问题(Python是面向对象语言,所以程序抛出的异常也是类)。能很好的理解这些错误提示类名所代表的意思,可以帮助你在最快的时间内找到问题所在,从而解决程序上的问题是非常有帮助的。
搜集了一些python最重要的内建异常类名,并做了简单的介绍:
AttributeError:属性错误,特性引用和赋值失败时会引发属性错误
NameError:试图访问的变量名不存在
SyntaxError:语法错误,代码形式错误
Exception:所有异常的基类,因为所有python异常类都是基类Exception的其中一员,异常都是从基类Exception继承的,并且都在exceptions模块中定义。
IOError:一般常见于打开不存在文件时会引发IOError错误,也可以解理为输出输入错误
KeyError:使用了映射中不存在的关键字(键)时引发的关键字错误
IndexError:索引错误,使用的索引不存在,常索引超出序列范围,什么是索引
TypeError:类型错误,内建操作或是函数应用在了错误类型的对象时会引发类型错误
ZeroDivisonError:除数为0,在用除法操作时,第二个参数为0时引发了该错误
ValueError:值错误,传给对象的参数类型不正确,像是给int()函数传入了字符串数据类型的参数。
1)忘记在if,elif, else,for,while,class ,def声明末尾添加:(导致 “SyntaxError :invalid syntax”)
该错误将发生在类似如下代码中:
if spam == 42
print('Hello!')
2)使用=而不是==(导致“SyntaxError:invalidsyntax”)
=是赋值操作符而==是等于比较操作。该错误发生在如下代码中:
if spam = 42:
print('Hello!')
3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)
记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
print('Howdy!')
或者:
if spam == 42:
print('Hello!')
4)尝试修改string的值(导致“TypeError:'str'objectdoesnotsupportitemassignment”)
string是一种不可变的数据类型,该错误发生在如下代码中:
spam = 'I have apet cat.'
spam[13] = 'r'
print(spam)
而你实际想要这样做:
spam = 'I have apet cat.'
spam = spam[:13] +'r' + spam[14:]
print(spam)
5)在字符串首尾忘记加引号(导致“SyntaxError:EOLwhilescanningstringliteral”)
该错误发生在如下代码中:
print(Hello!')
或者:
print('Hello!)
或者:
myName = 'Al'
print('My name is '+ myName + . How are you?')
6)变量或者函数名拼写错误(导致“NameError:name'fooba'isnotdefined”)
该错误发生在如下代码中:
foobar = 'Al'
print('My name is '+ fooba)
或者:
spam = ruond(4.2)
或者:
spam = Round(4.2)
7)尝试使用Python关键字作为变量名(导致“SyntaxError:invalid syntax”)
Python关键不能用作变量名,该错误发生在如下代码中:
class = 'algebra'
Python3的关键字有:and, as, assert, break, class, continue, def, del, elif,else, except, False, finally, for, from, global, if, import, in, is, lambda,None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
592

被折叠的 条评论
为什么被折叠?



