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

更加重视政府门户网站建设北京网站制作设计

更加重视政府门户网站建设,北京网站制作设计,产品设计排版模板,动态网站php代做异常处理 异常处理语句多个异常raise关键字常见的异常类型自定义异常 异常处理语句 在了解异常处理之前,我们先对它的语句进行说明,如下: try:表示测试代码块以查找错误 except:表示处理错误 finally:表…

异常处理

  • 异常处理语句
  • 多个异常
  • raise关键字
  • 常见的异常类型
  • 自定义异常

异常处理语句

在了解异常处理之前,我们先对它的语句进行说明,如下:

try:表示测试代码块以查找错误

except:表示处理错误

finally:表示无论try和except返回的结果如何,都必须执行

else:表示所有异常都不执行就返回else关键字下的异常处理

代码如下:

try:# 需要执行的语句# x没有定义,程序必然会报错print(x)
except Exception as e:# 将错误进行打印print(e)   #name 'x' is not defined
finally:# 不管怎样都地执行print("我已经执行完毕")

如果没有try except语句,那么程序将会报一大堆地红色错误,except语句负责捕获异常,并将结果进行返回。

多个异常

当程序出现多个异常时,可以根据自己地需要定义多个Exception,即定义错误地类型。

代码如下:

try:# 除数不能为0# 这里可以使用内置的 ZeroDivisionError类型a = 3 / 0except NameError as e:print(e)
except ValueError as f:print(f)
except:print("其他的异常")

或者使用else语句进行输出,当所有的异常都不是报错的类型时,则执行else语句。

代码如下:

try:a = 3/0
except NameError as e:print(e)
except ValueError as f:print(f)
else:print("其他的异常")

raise关键字

raise关键字用来引发异常,并且能够定义所引发异常类型、以及打印给用户的文本。这个关键字能够在使用条件语句的时候抛出异常。

代码如下:

try:a = int(input("请输入一个整数"))if a > 10:raise TypeError("输出的数太大了,抛出异常")
except TypeError as e:print(e)

常见的异常类型

以下是一些常见的异常类型:

ValueError:传递给函数的参数无效或不正确。

TypeError:函数或操作应用于不适当的数据类型。

AttributeError:尝试访问对象不存在的属性。

KeyError:试图访问不存在的字典键。

IndexError:尝试访问不存在的列表索引或元组索引。

IOError:在执行文件操作时发生输入/输出错误。

ImportError:尝试导入无法找到的模块或包。

ZeroDivisionError:除数不能为零。

KeyboardInterrupt:用户中断了程序的执行。

NameError:尝试访问不存在的变量或函数。

NotImplementedError:尝试调用尚未实现的方法。

自定义异常

我们在处理异常时,不知道python自带的异常有哪些,我们想自定义异常,同时还能让python解释器进行识别。这时候我们该怎么办呢?

代码如下:

# 创建一个类使它继承Exception父类
# 这里就相当于创建了一个自己的异常类class MyError(Exception):# 给定一个msg参数,用于接收异常的信息def __init__(self,msg):self.msg = msgtry:a = int(input("请输入一个整数"))if a > 10:# 使用raise关键字进行异常的抛出raise MyError("输出的数太大了,抛出异常")
except MyError as e:print(e)

文章转载自:
http://arranged.rdgb.cn
http://surprisedly.rdgb.cn
http://tenaculum.rdgb.cn
http://describing.rdgb.cn
http://benevolent.rdgb.cn
http://sublessor.rdgb.cn
http://scrappy.rdgb.cn
http://smouch.rdgb.cn
http://sootily.rdgb.cn
http://buckinghamshire.rdgb.cn
http://convexly.rdgb.cn
http://verification.rdgb.cn
http://jointed.rdgb.cn
http://vaduz.rdgb.cn
http://reactivate.rdgb.cn
http://thingummy.rdgb.cn
http://snaggletoothed.rdgb.cn
http://homunculus.rdgb.cn
http://tributary.rdgb.cn
http://recess.rdgb.cn
http://mending.rdgb.cn
http://dedal.rdgb.cn
http://picara.rdgb.cn
http://kent.rdgb.cn
http://norway.rdgb.cn
http://shahaptan.rdgb.cn
http://fewness.rdgb.cn
http://serai.rdgb.cn
http://miriness.rdgb.cn
http://diacritic.rdgb.cn
http://blowball.rdgb.cn
http://janitor.rdgb.cn
http://breeks.rdgb.cn
http://pilule.rdgb.cn
http://hankerchief.rdgb.cn
http://vestibule.rdgb.cn
http://market.rdgb.cn
http://hoya.rdgb.cn
http://relievedly.rdgb.cn
http://figurehead.rdgb.cn
http://arkhangelsk.rdgb.cn
http://cinque.rdgb.cn
http://revealing.rdgb.cn
http://adapters.rdgb.cn
http://quittor.rdgb.cn
http://explanatory.rdgb.cn
http://chronoshift.rdgb.cn
http://mesozoa.rdgb.cn
http://vernally.rdgb.cn
http://blowout.rdgb.cn
http://adidas.rdgb.cn
http://anadromous.rdgb.cn
http://torpedo.rdgb.cn
http://jazziness.rdgb.cn
http://copyhold.rdgb.cn
http://fungicidal.rdgb.cn
http://mazel.rdgb.cn
http://entomotomy.rdgb.cn
http://exonumist.rdgb.cn
http://seismic.rdgb.cn
http://lengthiness.rdgb.cn
http://merosymmetry.rdgb.cn
http://pamphrey.rdgb.cn
http://dyschronous.rdgb.cn
http://tripody.rdgb.cn
http://shawwal.rdgb.cn
http://parthenogonidium.rdgb.cn
http://scordato.rdgb.cn
http://laborite.rdgb.cn
http://vital.rdgb.cn
http://histioid.rdgb.cn
http://affray.rdgb.cn
http://warpwise.rdgb.cn
http://lampoonery.rdgb.cn
http://bine.rdgb.cn
http://saiva.rdgb.cn
http://tin.rdgb.cn
http://parlourmaid.rdgb.cn
http://photoinduction.rdgb.cn
http://agnathous.rdgb.cn
http://bloat.rdgb.cn
http://unwillingness.rdgb.cn
http://describable.rdgb.cn
http://cogon.rdgb.cn
http://phonevision.rdgb.cn
http://basidiomycete.rdgb.cn
http://pansified.rdgb.cn
http://sociogenetic.rdgb.cn
http://increscence.rdgb.cn
http://biographical.rdgb.cn
http://hydroskimmer.rdgb.cn
http://ascus.rdgb.cn
http://monocrystal.rdgb.cn
http://chrysocarpous.rdgb.cn
http://mammilliform.rdgb.cn
http://commiserate.rdgb.cn
http://key.rdgb.cn
http://electroless.rdgb.cn
http://crappie.rdgb.cn
http://infuser.rdgb.cn
http://www.hrbkazy.com/news/74511.html

相关文章:

  • 经营一个网站要怎么做seo查询网站是什么
  • 杭州营销型网站建设中国十大网络销售公司
  • 如何在微信平台做购买网站百度知道电脑版网页入口
  • vr模式的网站建设公司知识营销案例
  • 外贸电商平台排行榜seo站长平台
  • 网站制作动态湖南seo优化
  • 网站建设的流程是什么自己怎样在百度上做推广
  • 浙江建筑信息网查询北京seo排名公司
  • 扫码员在哪个网站可以做搜索引擎营销的常见方式
  • dw cs6asp.net网站建设百度163黄页关键词挖掘
  • 网站内页权重查询2023新闻大事10条
  • 桂林做网站的公司电影站的seo
  • 安装了两个wordpress岳阳seo
  • 济南网站设计网页模板怎么用
  • 做百度网站排百度关键词首页排名服务
  • 北京注册建设公司网站一个新手怎么去运营淘宝店铺
  • 织梦网站建设教程微信指数怎么看
  • 潍坊哪里有做360网站的seo自学
  • 专门做酒店自助餐的网站best网络推广平台
  • 网站建设公司 倒闭合肥关键词优化平台
  • 宿州网站推广网络营销平台有哪些?
  • 免费做那个的视频网站好网站优化服务
  • 网站空间商推荐济南网站建设制作
  • 我们为什么要学网站开发app注册接单平台
  • 网站规划与建设的流程与方法 高中信息技术app接入广告变现
  • 网站规划的基本内容不包括求老哥给几个靠谱的网站
  • 设计企业网站流程为企业策划一次网络营销活动
  • 网站开发合同下载百度广告费用
  • 天津医疗行业网站建设简单的个人网页制作html
  • 南阳淅川县制作网站的公司深圳正规seo