python常见问题及解决方法python错误异常怎么解决python常见错误以及自己的解决办法




python常见问题及解决方法python错误异常怎么解决python常见错误以及自己的解决办法

2022-07-20 22:20:45 网络知识 官方管理员

1.初识异常

1.1.什么是异常与异常处理

  • 异常就是错误
  • 异常会导致程序崩溃并停止运行
  • 能监控并捕获异常,将异常部位的程序进行修理使得程序继续正常运行

1.2.异常的语法

python常见问题及解决方法(python错误异常怎么解决)(1)

python常见问题及解决方法(python错误异常怎么解决)(2)

1#coding:utf-823defupper(str_data):4new_str='None'5try:6new_str=str_data.upper()7except:8print('程序出错了')9returnnew_str1011result=upper(123)12print(result)13'''14程序出错了15None16'''

1.3.捕获通用异常

python常见问题及解决方法(python错误异常怎么解决)(3)

1#coding:utf-823defupper(str_data):4new_str='None'5try:6new_str=str_data.upper()7exceptExceptionase:8print('程序出错了:{}'.format(e))9returnnew_str1011result=upper(123)12print(result)13'''14程序出错了:'int'objecthasnoattribute'upper'15None16'''

1.4.捕获具体异常

python常见问题及解决方法(python错误异常怎么解决)(4)

1#coding:utf-823deftest():4try:5print("trystart")61/07print("tryfinish")#这句代码不会执行,因为1/0已经报错了,直接进入到exception中了8exceptZeroDivisionErrorase:9print(e)10test()11'''12trystart13divisionbyzero14'''
1#coding:utf-823deftest():4try:5print("trystart")6print(name)7print("tryfinish")8exceptZeroDivisionErrorase:#except捕获的异常要与实际发生的异常一致,才能捕获到,不然没有用;捕获的是1/0的报错,实际发生的错误是name变量未定义9print(e)10test()11'''12Traceback(mostrecentcalllast):13File"D:/WorkSpace/Python_Study/test01.py",line10,in<module>14test()15File"D:/WorkSpace/Python_Study/test01.py",line6,intest16print(name)17NameError:name'name'isnotdefined18trystart19'''

1.5.捕获多个异常

python常见问题及解决方法(python错误异常怎么解决)(5)

python常见问题及解决方法(python错误异常怎么解决)(6)

2.python内置异常函数

异常类型集合

异常名称

说明

Exception

通用异常类型(基类)

ZeroDivisionError

不能整除0

AttributeError

对象没有这个属性

IOError

输入输出操作失败

IndexError

没有当前的索引

KeyError

没有这个键值(key)

NameError

没有这个变量(未初始化对象)

SyntaxError

Python语法错误

SystemError

解释器的系统错误

VauleError

传入的参数错误

3.异常中的finally

finally的功能:

  • 无论是否发生异常,一定会执行的代码块
  • 在函数中,即便在try或except中进行了return也依然会执行finally语法快
  • try语法至少要伴随except或finally中的一个
  • 语法:try:<>except:<>finally:<>ViewCode
1#coding:utf-823deftest():4try:5return"try"6except:7return"except"8finally:9return"finally"1011print(test())#finally

4.自定义异常

4.1.自定义抛出异常raise

raise:将信息以报错的形式抛出

python常见问题及解决方法(python错误异常怎么解决)(7)

python常见问题及解决方法(python错误异常怎么解决)(8)

1#coding:utf-823deftest(number):4ifnumber==100:5raiseValueError("number不能等于100")6returnnumber7print(test(50))#508print(test(100))9'''10Traceback(mostrecentcalllast):11File"D:/WorkSpace/Python_Study/test01.py",line8,in<module>12print(test(100))13File"D:/WorkSpace/Python_Study/test01.py",line5,intest14raiseValueError("number不能等于100")15ValueError:number不能等于10016'''
1#coding:utf-823#知识点1:raise主动抛出的异常可以被正常捕获4deftest(number):5ifnumber==100:6raiseValueError("number不能等于100")7returnnumber89deftest1(number):10try:11returntest(number)12exceptValueErrorase:13returne1415print(test1(100))#number不能等于100
1#coding:utf-823#知识点2:raise语法后面没有进行异常类型的传递,直接跟字符串,编译器报错,必须得加,如果不知道异常类型,使用基类Exception4deftest():5raise"报错了"67print(test())8'''9Traceback(mostrecentcalllast):10File"D:/WorkSpace/Python_Study/test01.py",line7,in<module>11print(test())12File"D:/WorkSpace/Python_Study/test01.py",line5,intest13raise"报错了"14TypeError:exceptionsmustderivefromBaseException15'''

4.2.自定义异常类

python常见问题及解决方法(python错误异常怎么解决)(9)

1#coding:utf-823classNumberLimitError(Exception):4def__init__(self,message):5self.message=message67classNameLimitError(Exception):8def__init__(self,message):9self.message=message1011deftest(name):12ifname=="张三":13raiseNameLimitError("张三不可以被填写")14returnname1516deftest1(number):17ifnumber>100:18raiseNumberLimitError("数字不能大于100")19returnnumber2021try:22test("张三")23exceptNameLimitErrorase:24print(e)25'''26张三不可以被填写27'''2829try:30test1(105)31exceptNumberLimitErrorase:32print(e)33'''34数字不能大于10035'''

5.断言

5.1.断言的功能---assert

assert断言:用于判断一个表达式,在表达式条件为false时触发异常

  • raise是生硬的抛出异常
  • assert是先进行判断然后根据结果决定是否抛出

5.2.断言得用法---assert

python常见问题及解决方法(python错误异常怎么解决)(10)

python常见问题及解决方法(python错误异常怎么解决)(11)

1#coding:utf-823deftest(name):4assertnamein["张三","李四"],"{}不在学生列表中".format(name)5print("{}在学生列表中".format(name))6test("张三")#张三在学生列表中7test("王五")8'''9Traceback(mostrecentcalllast):10File"D:/WorkSpace/Python_Study/test01.py",line7,in<module>11test("王五")12File"D:/WorkSpace/Python_Study/test01.py",line4,intest13assertnamein["张三","李四"],"{}不在学生列表中".format(name)14AssertionError:王五不在学生列表中15'''

6.python中的调试方法

百度上面的方法一大堆,这里我就不多赘述了。

发表评论:

最近发表
网站分类
标签列表