异常处理

1、语法错误:语法不按照Python的语法规则定义。

TypeError: string indices must be integers--字符串必须接受整型(用eval解决)

TypeError: change() takes 0 positional arguments but 1 was given--类型错误:change()接受0个位置参数,但给出了1个

TypeError: 'int' object is not subscriptable--TypeError是类型错误、“int”对象不可订阅

2、逻辑错误:例如1/0--ZeroDivisionError: division by zero(除数不能为零) 

if 判断式的异常处理只能针对某一段代码,对于不同的代码段的相同类型的错误你需要写重复的if判断来进行处、可读性极其的差、

3、AttributeError 试图访问一个对象没有的属性、比如foo.x ,但是foo没有熟属性

4、IOError 输入/输出异常,基本上是无法打开文件

5、ImportError 无法引入模块或包、基本上是路径问题或名称错误

6、IndentationError 语法错误(的子类)代码没有正确对齐

7、IndexError 下标索引超出序列边界、比如当x只有三个元素,却试图访问x[5]

8、KeyError 试图访问字典里不存在的键

9、KeyboardInterrupt CTRL+C被按下

10、NameError 使用一个还未被赋予对象的变量

11、SyntaxError python 代码非法,代码不能编译

12、TypeError 传入对象类型与要求的不符合

13、UnboundLocalError 试图访问一个还未被设置的局部变量,基本上是由于另有一个同名的全局变量导致以为正在访问他

14、ValueError 传入一个调用者不期望的值,即使值的类型是正确的

15、try and except的用法单分支

try:
age = input("1>>:")
int(age) #主逻辑
num2 = input("2>>:")
int(num2)
except ValueError as e:
print(e)
16、try and except的用法多分支:
try:
age = input("1>>:")
int(age) #主逻辑
num2 = input("2>>:")
int(num2)
except KeyError:
pass
except ValueError as e:
print(e)
17、try and Exception万能异常(主逻辑出现问题时直接跳转到万能异常):
try:
age = input("1>>:")
int(age) #主逻辑
num2 = input("2>>:")
int(num2)
except Exception as e:
print(e)
18、try中的代码块没有异常时直接执行else里面的
s1 = "hello"
s1 = 1
try:
int(s1)
except IndexError as e:
print(e)
except KeyError as e:
print(e)
except ValueError as e:
print(e)
else:
print("try中的代码块没有异常执行我")
19、try和finally无论是否有异常都执行
s1 = "hello"
s1 = 1
try:
int(s1)
except IndexError as e:
print(e)
except KeyError as e:
print(e)
except ValueError as e:
print(e)
else:
print("try中的代码块没有异常执行我")
finally:
print("无论是否有异常都可以捕捉到,主要是进行清理工作")
20、assert断言
21、PEP 8:multiple spaces after keyword--关键字后有多个空格
      
missing whitespace around operator--运算符周围缺少空格
        whitespace before--前空白
        blank line at end of file--文件末尾的空行(解决办法:文件尾部需要加一个空白行)
        Ignore errors like this--忽略这样的错误
        reformat file--重新格式化文件
22、SyntaxError: unexpected EOF while parsing--语法错误:分析时出现意外的eof
23、
statement seems to have no effect--声明似乎没有效果
24、IDE and Plugin Updates--IDE和插件更新
25、popup--弹出
26、

posted @ 2019-06-07 17:57  干it的小张  阅读(642)  评论(0编辑  收藏  举报