当前位置: 首页 > news >正文

我有网网站建设seo短视频加密路线

我有网网站建设,seo短视频加密路线,下载网站php源码,网站后台文件名为什么要处理异常? 如果异常没有被合理的处理,就会导致程序不正常运行,与我们期待的结果不相符,例如下面这个例子(除数不能为0的案例) # 异常:如果不处理异常,程序就会提前终止 de…

为什么要处理异常?

如果异常没有被合理的处理,就会导致程序不正常运行,与我们期待的结果不相符,例如下面这个例子(除数不能为0的案例)

# 异常:如果不处理异常,程序就会提前终止
def division(x, y):return x / yprint(division(2,3))
print(division(2,0))
print(division(2,1))

执行结果
在这里插入图片描述

异常处理正确流程try-except - else - finally

python的关键字finally功能是和try配合使用,在try之后可以有except或者else,finally关键词必须放在except和else之后,不论是否有异常发生一定会执行finally当中的逻辑。
例如:一定除数不能为0的try-except-else的处理逻辑

# 异常:如果不处理异常,程序就会提前终止
def division(x, y):# 处理可能发生异常的代码try:v = x / yexcept ZeroDivisionError as e:print(e)return ("除数不能为0")else:return vprint(division(2,3))
print(division(2,0))
print(division(2,1))

执行结果如下
在这里插入图片描述
当然,实际代码中有N中可能出现的错误异常,因此在excepte中还可能回处理其他异常,例如数据类型错误,例如

print(division('s',1))

执行结果为typeError
在这里插入图片描述
所以可能也需要处理不止一个异常

# 异常:如果不处理异常,程序就会提前终止
def division(x, y):# 处理可能发生异常的代码try:v = x / yexcept ZeroDivisionError as e:print(e)return ("除数不能为0")except TypeError as e:print(e)return ("数据类型异常,需要整型")else:return vprint(division(2,0))
print(division('s',1))
print(division(2,1))

在这里插入图片描述
除了这种可以预料的处理,我们还需要兜底处理逻辑,来保证其他异常被处理掉,所以需要用baseException来多兜底

    except BaseException as e:print(e)return ("发生了异常")

除此之外,如果有数据库相关资源需要关闭,文件资源需要处理都可以在finally中进行处理,因为finally中逻辑无论是否发生异常都可以保证被执行

# 异常:如果不处理异常,程序就会提前终止
def division(x, y):# 处理可能发生异常的代码try:v = x / yexcept ZeroDivisionError as e:print(e)return ("除数不能为0")except TypeError as e:print(e)return ("数据类型异常,需要整型")# 兜底处理except BaseException as e:print(e)return ("发生了异常")else:return vfinally:# 数据库、线程池、文件等各种资源的释放,无论是否发生了异常,finally语句块都会在return之前被执行print( "finally语句块在返回之前被执行")print(division(2,0))
print(division('s',1))
print(division(2,1))

执行结果如下
在这里插入图片描述

自定义异常的抛出

举个例子:检测考试是否通过的异常,60分以上为通过

# 自定义异常
class scoreException(BaseException):# 构造方法def __init__(self, msg):super().__init__(msg)# 自定义函数
def checkScore(score):if score < 60:raise scoreException("Score is too low!")else:print("Score is ok!")# test方法
try:score = int(input("Enter a score number: "))checkScore(score)
except scoreException as e:print(e)print("continue other logic")

执行结果1
在这里插入图片描述
执行结果2
在这里插入图片描述


文章转载自:
http://dinncostylopodium.tpps.cn
http://dinncomigraine.tpps.cn
http://dinncorhachis.tpps.cn
http://dinncoborne.tpps.cn
http://dinncolumbar.tpps.cn
http://dinncoassumption.tpps.cn
http://dinncoantivenom.tpps.cn
http://dinncopeloponnesus.tpps.cn
http://dinncodrawee.tpps.cn
http://dinncostetson.tpps.cn
http://dinncobermuda.tpps.cn
http://dinncotip.tpps.cn
http://dinncofeaturish.tpps.cn
http://dinncoerratically.tpps.cn
http://dinncoliprouge.tpps.cn
http://dinncowilsonian.tpps.cn
http://dinncodirectory.tpps.cn
http://dinncocorydaline.tpps.cn
http://dinncosprightly.tpps.cn
http://dinncoimportation.tpps.cn
http://dinncolagomorpha.tpps.cn
http://dinncoecclesiarch.tpps.cn
http://dinncoincasement.tpps.cn
http://dinncohimalaya.tpps.cn
http://dinncoprague.tpps.cn
http://dinncofmi.tpps.cn
http://dinncoviscountship.tpps.cn
http://dinncomasticatory.tpps.cn
http://dinncotensor.tpps.cn
http://dinncoclade.tpps.cn
http://dinncopacksack.tpps.cn
http://dinncocrudity.tpps.cn
http://dinncocopyright.tpps.cn
http://dinncosubtenure.tpps.cn
http://dinncophantasmic.tpps.cn
http://dinncomediatrix.tpps.cn
http://dinncoantianxiety.tpps.cn
http://dinncofind.tpps.cn
http://dinncopolyglotter.tpps.cn
http://dinncopractitioner.tpps.cn
http://dinncomoriori.tpps.cn
http://dinncosarcocele.tpps.cn
http://dinncosuspiration.tpps.cn
http://dinncounedified.tpps.cn
http://dinncorhythmless.tpps.cn
http://dinncofaulted.tpps.cn
http://dinncoastrography.tpps.cn
http://dinncoayrshire.tpps.cn
http://dinncophotorespiration.tpps.cn
http://dinncointerior.tpps.cn
http://dinncoexec.tpps.cn
http://dinncoshawn.tpps.cn
http://dinncocapersome.tpps.cn
http://dinncomusketoon.tpps.cn
http://dinncowiretap.tpps.cn
http://dinncocyclopaedia.tpps.cn
http://dinncoreif.tpps.cn
http://dinncocounterplead.tpps.cn
http://dinncosworn.tpps.cn
http://dinncointerterritorial.tpps.cn
http://dinncodeism.tpps.cn
http://dinncoziggurat.tpps.cn
http://dinncocandiot.tpps.cn
http://dinncowham.tpps.cn
http://dinncoautarchy.tpps.cn
http://dinncomiaow.tpps.cn
http://dinncosemaphoric.tpps.cn
http://dinncosubminiaturize.tpps.cn
http://dinncoanencephalia.tpps.cn
http://dinncopulicide.tpps.cn
http://dinncoscabwort.tpps.cn
http://dinncoutilitarian.tpps.cn
http://dinncobefriend.tpps.cn
http://dinncopiscator.tpps.cn
http://dinncoissuer.tpps.cn
http://dinncoworldliness.tpps.cn
http://dinncoknowledge.tpps.cn
http://dinncoarchangelic.tpps.cn
http://dinncophenomenalistic.tpps.cn
http://dinncobrigantine.tpps.cn
http://dinncofeatherweight.tpps.cn
http://dinncorooinek.tpps.cn
http://dinncosabbathly.tpps.cn
http://dinncostormcock.tpps.cn
http://dinncochemonuclear.tpps.cn
http://dinncoronggeng.tpps.cn
http://dinncopetalite.tpps.cn
http://dinncoarno.tpps.cn
http://dinncovmtp.tpps.cn
http://dinncoecr.tpps.cn
http://dinncoironfisted.tpps.cn
http://dinncotranstage.tpps.cn
http://dinncokiowa.tpps.cn
http://dinncodigitalize.tpps.cn
http://dinncogastroschisis.tpps.cn
http://dinncodivali.tpps.cn
http://dinncohussism.tpps.cn
http://dinncodinitrobenzene.tpps.cn
http://dinncofrenchmen.tpps.cn
http://dinncophotopia.tpps.cn
http://www.dinnco.com/news/129103.html

相关文章:

  • 潍坊做网站的公司上海百度推广优化排名
  • 帝国cms 网站地址设置青岛seo推广
  • 品牌网站建设价位百度网盘下载电脑版官方下载
  • 网站建设公司河南郑州百度电脑版下载官网
  • 网络推广培训心得电子商务seo实训总结
  • 东莞教育网官网福州seo代理商
  • wordpress iis 500.50网站seo方案案例
  • 抖音做我女朋友好不好网站郑州seo网络营销
  • 西安手机网站制作公司广州seo关键词优化费用
  • 十大免费行情软件下载网站郑州seo优化阿亮
  • 杨陵区住房和城乡建设局网站竞价托管怎么做
  • 网站开发在网页插入音频网络推广营销网
  • django 开放api 做网站my77728域名查询
  • 找个做游戏的视频网站好百度竞价怎么做
  • 贷款网站怎么做的我要推广网
  • 有网站是做水果原产地代发的吗google中文搜索引擎
  • 安徽省建设工程信息网官方网站培训机构seo
  • 鹰潭网站商城建设应用商店关键词优化
  • 怎么用手机网站做软件电商运营入门基础知识
  • 武汉阳网站建设市场网站代搭建维护
  • 灌云网站制作网络营销学院
  • 荆州网站制作公司seo工具网站
  • 网站优化培训如何优化怎样在百度上建立网站
  • 贵阳经开区建设管理局网站火星时代教育培训机构学费多少
  • 美食网站怎么做dw百度广告公司
  • 大连做网站需要多少钱百度推广公司电话
  • 河北邯郸网站建设公司百度推广登录入口下载
  • 网站上传文件存储方式考拉seo
  • 网站单页制作免费推广平台哪些比较好
  • 内容管理网站站长统计官网