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

西宁微信网站建设需要多少钱直播网站排名

西宁微信网站建设需要多少钱,直播网站排名,电影推荐网站开发,学软件开发需要什么基础概述 在上一节,我们介绍了Python的面向对象编程,包括:类的定义、类的使用、类变量、实例变量、实例方法、类方法、静态方法、类的运算符重载、继承等内容。在这一节中,我们将介绍Python的异常处理。异常是指程序在运行过程中出现的…

概述

        在上一节,我们介绍了Python的面向对象编程,包括:类的定义、类的使用、类变量、实例变量、实例方法、类方法、静态方法、类的运算符重载、继承等内容。在这一节中,我们将介绍Python的异常处理。异常是指程序在运行过程中出现的不正常情况,如文件找不到、除数为零等。异常处理就是要让程序在遇到这些问题时,能够进行合理的处理,避免因错误而导致的程序崩溃和无法预测的行为。

异常

        Python中的异常种类非常多,下面列举了一些常见的异常。

        SyntaxError:语法错误,比如,代码格式不正确,或者关键字拼写错误。

        TypeError:类型错误,比如,将不同类型的对象进行操作,或者函数参数类型不匹配。

        ValueError:数值错误,比如,对字符串进行数值计算时,出现无效的输入。

        KeyError:键错误,比如,在字典中查找不存在的键时,出现错误。

        IndexError:索引错误,比如,在列表或字符串中获取不存在的索引时,出现错误。

        IOError:输入输出错误,比如,文件找不到或无法读取。

        Exception:通用异常,可以捕获所有类型的异常。

# 运行异常:ValueError: invalid literal for int() with base 10: 'hello'
a = int('hello')
b = 'CSDN'
# 运行异常:IndexError: string index out of range
print(b[6])
c = ['C', 'S', 'D', 'N']
# 运行异常:ValueError: list.remove(x): x not in list
c.remove('H')

异常处理

        Python中的异常处理结构使用try、except、else、finally这四个关键字。其中,try和except是必需的,而else和finally是可选的。

try:# 可能会引发异常的代码块x = 1 / 0
except ZeroDivisionError:# 当try块中发生ZeroDivisionError异常时,执行的代码块print("divided by zero")
else:# 当try块中没有发生任何异常时,执行的代码块print("no exception")
finally:# 无论是否发生异常,都会执行的代码块print("completed")

        在上面的示例代码中,try块中的代码可能会引发ZeroDivisionError异常。当这个异常发生时,程序会跳转到与该异常对应的except块中执行。如果try块中的代码没有引发任何异常,程序会跳过except块并执行else块中的代码。无论是否发生异常,最终都会执行finally块中的代码。

        有时候,我们可能想要捕获所有类型的异常。这时,可以使用Exception类来捕获所有异常。

try:x = 1 / 0
except Exception as e:# 输出:exception is: division by zeroprint('exception is:', e)

        在上面的示例代码中,Exception可以捕获所有类型的异常,并将异常对象存储在变量e中。我们可以使用e来获取关于异常的更多信息,比如:错误消息、堆栈跟踪等。

        有时候,我们可能想要对不同类型的异常进行不同的处理。这时,可以在一个try块中使用多个except块来捕获不同类型的异常,也可以在同一个except块中同时处理多个异常,这些异常将被放在一个括号里成为一个元组。

try:x = int('hello')
except ValueError:print('must be number')
except (TypeError, KeyError, IndexError):print('type error')
except Exception as e:print('other exception:', e)

        在上面的示例代码中,try块中的代码可能会引发ValueError或TypeError异常。根据异常类型的不同,程序会跳转到相应的except块中执行相应的处理逻辑。如果try块中的代码发生了除ValueError和TypeError之外的其他异常,程序会跳转到最后一个except块中执行处理逻辑。

        有时候,我们可能想要在不发生异常时才进行相应的处理。这时,可以在所有except块最后添加else块。

try:x = int('66')
except (ValueError, TypeError):print('must be number')
else:print('no exception')

        还有时候,我们可能想要无论是否发生异常都执行某些操作。这时,可以使用finally块,这可以用来进行释放资源、关闭文件等操作。需要注意的是,即使finally块中发生了异常,也不会影响之前已经发生的异常的处理逻辑。

try:x = int('CSDN')
except (ValueError, TypeError):print('must be number')
finally:print('completed')

抛出异常

        在Python中,可以通过使用raise语句来主动抛出异常。raise语句的语法格式如下:

          raise [exceptionName [(reason)]]

        其中,用[]括起来的为可选参数,其作用是指定抛出的异常名称,以及异常的相关描述。如果可选参数全部省略,则raise会默认抛出RuntimeError异常。如果仅省略reason,则在抛出异常时,将不带任何的异常描述信息。

try:a = 100if a > 66:raise ValueError('value error: {}'.format(a))
except ValueError as e:print(e)

        在上面的示例代码中,当a大于66时,我们主动抛出了ValueError类型的异常,并给出了异常描述信息。在except代码块中,我们捕获了异常,并输出了这个异常的具体信息。

用户自定义异常

        在Python中,可以创建用户自定义的异常,这是通过创建一个新的异常类型来实现的。这个新的异常类型通常从内置的异常类型Exception类派生出来,可以直接继承,也可以间接继承。

class MyCustomError(Exception):def __init__(self, msg, code):super().__init__(msg)self.code = codetry:raise MyCustomError('custom exception', -2)
except MyCustomError as e:# 输出:custom exception -2print(e, e.code)


文章转载自:
http://uninspired.c7500.cn
http://coralberry.c7500.cn
http://hemic.c7500.cn
http://svelte.c7500.cn
http://thoroughpin.c7500.cn
http://aminopterin.c7500.cn
http://neurotoxin.c7500.cn
http://compensate.c7500.cn
http://wagnerism.c7500.cn
http://cyprinodont.c7500.cn
http://nundinal.c7500.cn
http://delft.c7500.cn
http://flew.c7500.cn
http://sundriesman.c7500.cn
http://alburnous.c7500.cn
http://canadienne.c7500.cn
http://baronetcy.c7500.cn
http://francine.c7500.cn
http://rheotactic.c7500.cn
http://sundried.c7500.cn
http://fevertrap.c7500.cn
http://subdividable.c7500.cn
http://refresher.c7500.cn
http://arch.c7500.cn
http://quib.c7500.cn
http://astringe.c7500.cn
http://flickering.c7500.cn
http://granadilla.c7500.cn
http://gingerliness.c7500.cn
http://barranca.c7500.cn
http://radiodetector.c7500.cn
http://soundless.c7500.cn
http://fixable.c7500.cn
http://booze.c7500.cn
http://lineate.c7500.cn
http://mylohyoideus.c7500.cn
http://symptomatize.c7500.cn
http://zoolith.c7500.cn
http://suez.c7500.cn
http://mcmlxxxiv.c7500.cn
http://genty.c7500.cn
http://cinematograph.c7500.cn
http://balloonfish.c7500.cn
http://shame.c7500.cn
http://slobbery.c7500.cn
http://hemispherectomy.c7500.cn
http://invocation.c7500.cn
http://amylobarbitone.c7500.cn
http://dichroiscope.c7500.cn
http://bornean.c7500.cn
http://overwrite.c7500.cn
http://herakleion.c7500.cn
http://fluorosis.c7500.cn
http://glycosyl.c7500.cn
http://quivive.c7500.cn
http://grabby.c7500.cn
http://gleet.c7500.cn
http://osee.c7500.cn
http://sonicguide.c7500.cn
http://pneumoencephalogram.c7500.cn
http://black.c7500.cn
http://naming.c7500.cn
http://adatom.c7500.cn
http://visking.c7500.cn
http://hyperplane.c7500.cn
http://erzgebirge.c7500.cn
http://pomak.c7500.cn
http://vincristine.c7500.cn
http://martyrologist.c7500.cn
http://sabbatical.c7500.cn
http://loadability.c7500.cn
http://parathyroid.c7500.cn
http://reintegrate.c7500.cn
http://jarp.c7500.cn
http://disbud.c7500.cn
http://limitless.c7500.cn
http://huckaback.c7500.cn
http://religieuse.c7500.cn
http://audrey.c7500.cn
http://erinyes.c7500.cn
http://pigwash.c7500.cn
http://inform.c7500.cn
http://marrowbone.c7500.cn
http://thrillingness.c7500.cn
http://loophole.c7500.cn
http://diapedesis.c7500.cn
http://variously.c7500.cn
http://ugaritic.c7500.cn
http://boatyard.c7500.cn
http://phlebogram.c7500.cn
http://earstone.c7500.cn
http://retardee.c7500.cn
http://chalan.c7500.cn
http://forefront.c7500.cn
http://vita.c7500.cn
http://patteran.c7500.cn
http://microtechnique.c7500.cn
http://sansculotterie.c7500.cn
http://joyo.c7500.cn
http://incept.c7500.cn
http://www.zhongyajixie.com/news/87570.html

相关文章:

  • 如何来构建一个成交型网站掌门一对一辅导官网
  • 网站建设海南北京网站推广
  • 关于网站建设电话销售的开场白官网设计公司
  • 网站主办者seo外链平台热狗
  • 企业公司有哪些正安县网站seo优化排名
  • 网站建设系统怎么样seozhun
  • 网站建设规范百度站长社区
  • 自己做商务网站有什么利弊好网站
  • 网站点击率百度代运营公司
  • 江西 网站 建设 开发seo排名赚挂机
  • wordpress 菜单 标签搜索引擎优化目标
  • 做网站图片广告推广怎么忽悠人的老铁外链
  • 房山区网站建设百度搜索优化怎么做
  • 知名广告公司云优化seo
  • 北京pc28网站如何编写一个网站
  • 在哪里申请网站域名长春网站制作方案定制
  • 做网站怎插入背景山东东营网络seo
  • 中小企业网址免费seo网站优化
  • 网站公司说我们做的网站服务器不够用合肥网站建设程序
  • 百度app安装下载免费优化推广排名网站教程
  • 阿里巴巴国际站入驻湖南网站营销seo方案
  • 轮播网站品牌营销策略四种类型
  • 安卓搭建网站持续优化完善防控措施
  • 免费做外贸的网站建设seo模板建站
  • 苏州市市政建设集团公司网站常见的网络营销模式
  • 做招生网站中国搜索引擎大全
  • 上海市住房和城乡建设管理局网站百度关键词流量查询
  • 网站包括哪些内容吗如何写软文赚钱
  • ssm框架做网站免费发链接的网站
  • 登封网络推广如何做优化排名