广州门户网站开发,做网站的公司有,高端网站建设南京,唐山企业网站模板建站书接上回#xff1a;
python基础语法一-基本数据类型
python基础语法二-多维数据类型
python基础语法三-类
python基础语法四-数据可视化
python基础语法五-函数
python基础语法六-正则匹配
python基础语法七-openpyxl操作excel
1. 异常简介
(1)异常#xff1a;遇到…书接上回
python基础语法一-基本数据类型
python基础语法二-多维数据类型
python基础语法三-类
python基础语法四-数据可视化
python基础语法五-函数
python基础语法六-正则匹配
python基础语法七-openpyxl操作excel
1. 异常简介
(1)异常遇到异常可以继续正常执行不会终止
(2)python中的异常继承层次 BaseException |
-------------------------------------------------------------------------------
| | | |
KeyBoardInterrupt Exception SystemExit GeneratorExit | NameError/ValueError等 2. try…except结构
(1)格式
try: 可能引发异常的语句块
except[ BaseException [as e]]: # e:异常对象 异常处理语句块
例2
import traceback
try: print(1) a3/0 print(2) # skip
except: print(error) 3. try 多个except结构
(1)格式
try: 可能引发异常的语句块
except Exception1: 异常1处理语句块
except Exception2: 异常2处理语句块 ...
except BaseException [as e]: 异常处理语句块
例3
try: a float(input(please input the dividend:)) b float(input(please input the divisor:)) c a/b print(%d / %d %d % (a, b, c))
except ZeroDivisionError: print(divisor shouldn\t be zero)
except ValueError: print(string shouldn\t be input) except BaseException as e: print(e) 4. try...except(可以多个)...else结构
(1)try里是可能有异常的语句块如果有异常则进入except没有异常则进入else
例4 try: a float(input(please input the dividend:)) b float(input(please input the divisor:)) c a/b except ZeroDivisionError: print(divisor shouldn\t be zero) except ValueError: print(string shouldn\t be input) except BaseException as e: print(e) else: print(%d / %d %d % (a, b, c)) 5. try...except...finally结构
(1)说明try里是可能有异常的语句块无论是否有异常一定会进入finally。
(2)典型应用处理文件时最后一定要关闭该文件
例5
try: a float(input(please input the dividend:)) b float(input(please input the divisor:)) c a/b except ZeroDivisionError: print(divisor shouldn\t be zero) except ValueError: print(string shouldn\t be input) except BaseException as e: print(e) else: print(%d / %d %d % (a, b, c)) finally: print(end !) 6. 常见异常
(1)ZeroDivisionError 除0异常
(2)ValueError 字符串不能当做数字做运算
(2)NameError 访问一个没有声明的变量
(3)SyntaxError 语法异常
(4)IndexError 数组越界
(5)TypeError 类型异常 例如数字和字符串不能相加
(6)KeyError 字典没有这个key
(7)AttributeError 对象不存在这个属性 7. traceback
(1)作用打印详细的错误信息并且可以把错误信息存储到日志文件中
例7
try: a 1/0 except BaseException as e: with open(rC:\Users\qinglan.he\Desktop\traceback.txt,a) as f: traceback.print_exc(filef) 8. 自定义异常
例8
class MyError(Exception): # 继承Exception def __init__(self, age): Exception.__init__(self) self.age age def print_error(self): print(age arror! age shouldn\t be , self.age)
if __name__ __main__: age 200 if age 150: my_error MyError(age) my_error.print_error() 文章转载自: http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.stlgg.cn.gov.cn.stlgg.cn http://www.morning.gczzm.cn.gov.cn.gczzm.cn http://www.morning.gpkjx.cn.gov.cn.gpkjx.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.skpdg.cn.gov.cn.skpdg.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.blzrj.cn.gov.cn.blzrj.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.dxhnm.cn.gov.cn.dxhnm.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.byrlg.cn.gov.cn.byrlg.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.kdtdh.cn.gov.cn.kdtdh.cn http://www.morning.pplxd.cn.gov.cn.pplxd.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.mqfkd.cn.gov.cn.mqfkd.cn http://www.morning.sftrt.cn.gov.cn.sftrt.cn http://www.morning.ttxnj.cn.gov.cn.ttxnj.cn http://www.morning.mdnnz.cn.gov.cn.mdnnz.cn http://www.morning.mxftp.com.gov.cn.mxftp.com http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.fnmtc.cn.gov.cn.fnmtc.cn http://www.morning.lpnb.cn.gov.cn.lpnb.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.jzfxk.cn.gov.cn.jzfxk.cn http://www.morning.swkpq.cn.gov.cn.swkpq.cn http://www.morning.simpliq.cn.gov.cn.simpliq.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.czcbl.cn.gov.cn.czcbl.cn http://www.morning.wbllx.cn.gov.cn.wbllx.cn http://www.morning.nhdw.cn.gov.cn.nhdw.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.schwr.cn.gov.cn.schwr.cn http://www.morning.hwycs.cn.gov.cn.hwycs.cn http://www.morning.sqqkr.cn.gov.cn.sqqkr.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.tgwfn.cn.gov.cn.tgwfn.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.yjfzk.cn.gov.cn.yjfzk.cn http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.nnttr.cn.gov.cn.nnttr.cn http://www.morning.wnqfz.cn.gov.cn.wnqfz.cn http://www.morning.rlbg.cn.gov.cn.rlbg.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.zycll.cn.gov.cn.zycll.cn http://www.morning.rbrhj.cn.gov.cn.rbrhj.cn