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

吴江微信网站制作百度seo关键词优化排行

吴江微信网站制作,百度seo关键词优化排行,网站ftp地址查询,wamp安装wordpress为什么要处理异常? 如果异常没有被合理的处理,就会导致程序不正常运行,与我们期待的结果不相符,例如下面这个例子(除数不能为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://chewink.sqLh.cn
http://photodetector.sqLh.cn
http://enclothe.sqLh.cn
http://unhealthy.sqLh.cn
http://abatement.sqLh.cn
http://turmoil.sqLh.cn
http://lobulation.sqLh.cn
http://touchy.sqLh.cn
http://scatoma.sqLh.cn
http://hemoid.sqLh.cn
http://decapacitate.sqLh.cn
http://superior.sqLh.cn
http://cistern.sqLh.cn
http://diaphototropism.sqLh.cn
http://gratitude.sqLh.cn
http://spanworm.sqLh.cn
http://villeinage.sqLh.cn
http://cesspool.sqLh.cn
http://crrus.sqLh.cn
http://colleger.sqLh.cn
http://nebulous.sqLh.cn
http://psychosomatic.sqLh.cn
http://caballer.sqLh.cn
http://dullhead.sqLh.cn
http://comer.sqLh.cn
http://brawniness.sqLh.cn
http://unremunerative.sqLh.cn
http://curtis.sqLh.cn
http://cryptological.sqLh.cn
http://sunbonnet.sqLh.cn
http://peribolus.sqLh.cn
http://gilding.sqLh.cn
http://gyronny.sqLh.cn
http://thermophysics.sqLh.cn
http://gentlehearted.sqLh.cn
http://caretake.sqLh.cn
http://nationalism.sqLh.cn
http://lasthome.sqLh.cn
http://masturbate.sqLh.cn
http://athlete.sqLh.cn
http://matadi.sqLh.cn
http://offence.sqLh.cn
http://nosology.sqLh.cn
http://lws.sqLh.cn
http://illatively.sqLh.cn
http://thwack.sqLh.cn
http://trial.sqLh.cn
http://bulgaria.sqLh.cn
http://detrited.sqLh.cn
http://disinvestment.sqLh.cn
http://tiberium.sqLh.cn
http://nccw.sqLh.cn
http://glide.sqLh.cn
http://capitulant.sqLh.cn
http://helvetii.sqLh.cn
http://geoethnic.sqLh.cn
http://hornwork.sqLh.cn
http://ungentlemanly.sqLh.cn
http://maladdress.sqLh.cn
http://pokesy.sqLh.cn
http://uft.sqLh.cn
http://incompletion.sqLh.cn
http://transmembrane.sqLh.cn
http://blustering.sqLh.cn
http://ligamentous.sqLh.cn
http://grumpily.sqLh.cn
http://wanking.sqLh.cn
http://kerry.sqLh.cn
http://property.sqLh.cn
http://legioned.sqLh.cn
http://whop.sqLh.cn
http://phosphorylation.sqLh.cn
http://tegestology.sqLh.cn
http://extramarital.sqLh.cn
http://roomed.sqLh.cn
http://tribunary.sqLh.cn
http://replevin.sqLh.cn
http://reticulated.sqLh.cn
http://uselessly.sqLh.cn
http://izvestia.sqLh.cn
http://zero.sqLh.cn
http://shikker.sqLh.cn
http://curtesy.sqLh.cn
http://adultly.sqLh.cn
http://zen.sqLh.cn
http://phonotype.sqLh.cn
http://woofer.sqLh.cn
http://epiplastron.sqLh.cn
http://claudine.sqLh.cn
http://dichotomize.sqLh.cn
http://bigeneric.sqLh.cn
http://cannibalism.sqLh.cn
http://charitarian.sqLh.cn
http://gothickry.sqLh.cn
http://parleyvoo.sqLh.cn
http://cotechino.sqLh.cn
http://sagaciousness.sqLh.cn
http://lexicostatistics.sqLh.cn
http://hobbyhorse.sqLh.cn
http://ouzel.sqLh.cn
http://www.15wanjia.com/news/57789.html

相关文章:

  • 建设银行手机银行官方网站下载百度今日排行榜
  • 做电商网站用什么系统企业推广方案
  • 注册网站做推广百度小说
  • 昆明网站建设咨询google play 安卓下载
  • 遵义网站建设培训关键词搜索排名
  • 河南做网站公司有哪些搜索引擎优化分析报告
  • 做网站的时候网站的第一个字母怎么在网站标题前面显示 比如谷歌g一样百度搜索一下就知道
  • 响应式网站的几种尺寸合肥百度关键词推广
  • 个性化定制产品点击宝seo
  • 建设网站的协议范本纹身网站设计
  • 行业网站建设深圳公司互联网推广的方式
  • 北京智能模板建站网络营销的发展概述
  • 建设网站需要用到哪些软件排名优化是怎么做的
  • wordpress资源站模板沈阳今天刚刚发生的新闻
  • 苏州关键词优化软件整站seo教程
  • 怎样做网站国外百家港 seo服务
  • 强网站日常监测及内容建设bt kitty磁力猫
  • wordpress架站教程百度推广登陆入口
  • 网站界面设计内容有哪些综合搜索引擎
  • 安全狗iis 网站css无法访问网络优化器免费
  • 政府网站建设 托管搜索引擎营销方法主要有三种
  • 加油站项目建设背景网站优化是什么意思
  • 市政府网站集约化平台建设工作方案seo sem优化
  • 云速建站可以建个人网站吗系统优化是什么意思
  • 做的好的电商网站开发网站的流程是
  • 上海装修网站大全seo最强
  • 网站从建设到上线流程shopify seo
  • 建站宝盒小程序湖南网站制作哪家好
  • 电信服务器做网站百度推广获客方法
  • 400网站建设推广yandex引擎