版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
程序设计错误1语法错误运行时错误逻辑错误6=x*2x=3/0area=2*3.14*3异常(Exception)Traceback(mostrecentcalllast):
File"<pyshell#0>",line1,in<module>1/0ZeroDivisionError:divisionbyzero2>>>1/0SourceTraceback(mostrecentcalllast):
File"<pyshell#1>",line1,in<module>y=x+1NameError:name'x'isnotdefined>>>y=x+1Source用异常对象(exceptionobject)表示异常情况异常查看异常类
dir(__builtins__)3类
名描
述BaseException所有异常的基类Exception常规异常的基类AttributeError对象不存在此属性IndexError序列中无此索引IOError输入/输出操作失败KeyboardInterrupt用户中断执行(通常输入Ctr-C)KeyError映射中不存在此键NameError找不到名字(变量)SyntaxErrorPython语法错误TypeError对类型无效的操作ValueError传入无效的参数ZeroDivisionError除(或取模)运算的第二个参数为0异常处理ify!=0:
print(x/y)else:
print('divisionbyzero')4VStry-except异常处理语句
捕捉异常9.25异常Enterthefirstnumber:aTraceback(mostrecentcalllast):
File"C:\Python\programs\exception1.py",line1,in<module>num1=int(input('Enterthefirstnumber:'))ValueError:invalidliteralforint()withbase10:'a'6#Filename:prog9-1.pynum1=int(input('Enterthefirstnumber:'))num2=int(input('Enterthesecondnumber:'))print(num1
/num2)File9.2.1try-except语句7try-except语句8#Filename:prog9-2.pytry:num1=int(input('Enterthefirstnumber:'))num2=int(input('Enterthesecondnumber:'))
print(num1/num2)except
ValueError:
print('Pleaseinputadigit!')Filetry:
被检测的语句块exceptException:
异常处理语句块try-except语句9#Filename:prog9-3.pytry:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except
ZeroDivisionError:
print('The
second
number
cannot
be
zero!')File9.2.2多个except子句和一个except块捕捉多个异常10多个except子句11#Filename:prog9-4.pytry:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except
ValueError:
print('Please
inputadigit!')except
ZeroDivisionError:
print('The
second
number
cannot
be
zero!')File一个except块捕捉多个异常12#Filename:prog9-5.pytry:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except(ValueError,ZeroDivisionError):
print('Invalid
input!')File空except子句13#Filename:prog9-6.pytry:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except:
print('Something
went
wrong!')File一了百了:except:as子句14#Filename:prog9-7.pytry:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except
Exception
aserr:
print('Something
went
wrong!')
print(err)Fileas子句15try:
被检测的语句块except异常类名
as错误原因名:
异常处理语句块print(错误原因名)9.2.3else子句16else子句17#Filename:prog9-8.pytry:num1=int(input('Enterthefirstnumber:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)except(ValueError,ZeroDivisionError):
print('Invalid
input!')else:
print('Aha,Iamsmart.')FileEnterthefirstnumber:3Enterthesecondnumber:50.6Aha,Iamsmart.加入循环18#Filename:prog9-9.pywhileTrue:
try:num1=int(input('Enterthefirstnumber:'))
num2=int(input('Enter
the
second
number:'))
print(num1/num2)
except
ValueError:
print('Please
input
adigit!')
except
ZeroDivisionError:
print('The
second
number
cannot
be
zero!')
else:
breakFileEnterthefirstnumber:aPleaseinputadigit!Enterthefirstnumber:3Enterthesecondnumber:0Thesecondnumbercannotbezero!Enterthefirstnumber:3Enterthesecondnumber:50.6break语句的位置19#Filename:prog9-9’.pywhileTrue:
try:num1=int(input('Enterthefirstnumber:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)
break
except
ValueError:
print('Please
input
adigit!')
except
ZeroDivisionError:
print('The
second
number
cannot
be
zero!')File改写prog9-9.pybreak语句的位置20#Filename:prog9-11.pywhileTrue:try:num1=int(input('Enter
the
first
number:'))num2=int(input('Enter
the
second
number:'))
print(num1/num2)
break
exceptExceptionaserr:
print(err)Filebreak语句的位置21#Filename:prog9-12.pyaList=[1,2,3,4,5]i=0whileTrue:
try:
print(aList[i])
except
IndexError:
print('index
error')
break
else:i+=1File9.2.4finally子句22finally子句23#Filename:prog9-13.pydeffinallyTest():
try:x=int(input('Enterthefirstnumber:'))
y=int(input('Enter
the
second
number:'))
print(x/y)
return1
except
Exception
aserr:
print(err)
return0
finally:
print('It
isafinallyclause.')result=finallyTest()print(result)FileEnterthefirstnumber:3Enterthesecondnumber:50.6Itisafinallyclause.1Enterthefirstnumber:3Enterthesecondnumber:0divisionbyzeroIti
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 搬运仓储物流设备年终大促宣传及营销方案
- 2026年氟化工设备维护模拟题
- 2026年Python初级脚本工程师笔试题
- 2026年服装设计师招聘面试设计预测题
- 2026年数据处理专员初级题库
- 山东省泰安市肥城市2025-2026学年高一下学期期中考试语文试卷(含答案)
- 2026年数据治理师初级模拟测试题集
- 2026年计量师一级仿真题分类解析与预测
- 2026年面试职业生涯规划
- 2026年注册会计师考试经济法仿真题集
- 2026年建筑施工焊工考试试题及答案
- (2026版)《煤矿重大事故隐患判定标准》培训课件
- 2025年湖北省中考生物、地理合卷试卷真题(含答案)
- 24春国家开放大学《客户关系管理》形考作业1-4参考答案
- 矿山系统机电技术人员考试题库
- GB/T 43232-2023紧固件轴向应力超声测量方法
- 单层厂房抗震设计
- 公路水运工程施工企业(主要负责人和安全生产管理人员)考核大纲及模拟题库
- 仓库收货部文员绩效考核办法
- 中华人民共和国尘肺标准片
- 教育部高中语文新课程标准
评论
0/150
提交评论