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

替别人做设计的网站石家庄网络营销网站推广

替别人做设计的网站,石家庄网络营销网站推广,论坛创建,临安农家乐做网站文章目录 前言一、异常是什么?二、异常处理1. 根据提示2. 捕获异常3.抛出异常——raise4.应用场景 总结 前言 我们在日常编写代码的时候,难免会遇到一些不可控的错误,这无疑会导致程序的终止,大大降低了程序的实用性,…

文章目录

  • 前言
  • 一、异常是什么?
  • 二、异常处理
    • 1. 根据提示
    • 2. 捕获异常
    • 3.抛出异常——raise
    • 4.应用场景
  • 总结


前言

我们在日常编写代码的时候,难免会遇到一些不可控的错误,这无疑会导致程序的终止,大大降低了程序的实用性,那么我们如何能优化这种情况呢?接下俩就让我们来详细了解一下吧!


一、异常是什么?

在Python中,异常是指在程序执行过程中出现的错误或意外情况。当Python解释器遇到异常时,会中断当前的执行流程,并尝试寻找异常处理程序来处理异常。如果找不到合适的异常处理程序,程序将终止并打印出错误信息。

分类

  1. 语法错误:
    syntaxerror:代码不符合Python语法规定
    ZeroDivisionError:除数为0
    KeyError:字典中不存在这个键
    AttributeError:对象没有这个属性
    TypeError:类型错误,传入的类型不匹配
    ImportError:无法引入模块或包,基本上是路径问题或名称错误

  2. 逻辑错误
    nameerror:使用一个还没有被定义的变量
    indexerror:下标/索引超出范围
    IOError:输入/输出操作错误,基本上是无法打开文件(比如你要读的文件不存在)
    ValueError:传入的值有误

二、异常处理

1. 根据提示

traceback找出错误点,并改正
xxxError:会显示异常的类型,以及具体的提示

2. 捕获异常

要保证程序的容错性和可靠性,遇到错误不直接崩溃,而是有对应的异常机制处理

  1. 捕获异常一
    语法:
    try:
      被检测的代码块
    except 异常类型 as e:
      检测到异常要执行的代码块

代码如下:

a = int(input('请输入第一个数字:'))
b = int(input('请输入第二个数字:'))
try:print(a/b)
except ZeroDivisionError as error:print(error)print('您输入的数据有误!')
# 执行的原理:执行try里的子代码块,如果字代码块没有触发异常,直接跳过except语句,try语句执行完毕
# 如果执行try发生异常,则跳过异常语句,执行except语句,except和指定的异常类型进行匹配,匹配成功就执行except代码,如果匹配不成功,异常没有捕获到,输出错误。
  1. 捕获异常二
    try检测的代码块出现了两个或多个异常,可以用多个except进行匹配,或者把多个异常类型放在一个元组内,用一个except匹配

代码如下:

try:a = int(input('请输入第一个数字:'))b = int(input('请输入第二个数字:'))print(a / b)
except (ZeroDivisionError, ValueError) as error:print(error)print('您输入的数据有误!')
except ValueError as e:print(e)  # invalid literal for int() with base 10: '10.0'
  1. 捕获异常三
    程序在执行出现错误的时候,出现逻辑错误,不能确定具体是什么逻辑错误,万能异常 Exception 代表所有异常类型
    作用:能捕获到大多数的异常,但是不能捕获语法错误

代码如下:

try:li = [1, 2, 3]print(li[0])a = int(input('请输入第一个数字:'))b = int(input('请输入第二个数字:'))print(a / b)
except Exception as e:print(e)

注意Exception的首字母必须要大写,不能捕获语法异常

  1. 捕获异常四
    当检测的代码块没有出现任何异常的时候,执行else代码

代码如下:

try:name = 'a'print(name)print(int('10'))print(float(10.14))
except Exception as e:print(e)
else:print('程序没有出现错误')
  1. 捕获异常五
    不管有没有检测到异常,都会执行 finally

代码如下:

'''语法:try:被检测的代码块except 异常类型 as e:检测到异常要执行的代码块else:没有捕获到异常执行的代码finally:无论是否有异常都会执行注意:finally和try可以单独连用作用:回收资源的操作,关闭已经打开的文件,关闭打开的数据库
'''try:name = 'a'print(name)print(int('10'))print(float(10.14))
except Exception as e:print(e)
else:print('程序没有出现错误')
finally:print('不管是否异常都会执行')  # 一般做一些文件的关闭

3.抛出异常——raise

前面都是不符合Python解释器的语法,由解释器抛出异常。我们也可以自己定义异常,在满足什么条件下主动抛出。

语法:
1.创建Exception(‘错误的信息’)
2.raise抛出即可
e = Exception(‘错误’)
raise e

代码如下:

def login():count = 0while True:username = input('请输入账号')code = input('请输入验证码')if code == '123456':count += 1if count == 5:e = Exception('验证码输入错误超过五次,请一分钟后重试')raise e
try:login()
except Exception as e:print(e)

4.应用场景

  1. 写程序:打开了一个文件,对文件执行读写操作,过程中遇到一些逻辑错误,引发了异常,通过try except进行捕获,注意写程序:打开了一个文件,对文件执行读写操作,过程中遇到一些逻辑错误,引发了异常,通过try except进行捕获。

代码如下:

def func():li = []return li[0]
func()
try:func()
except Exception as e:print(e)# 函数:处理某一个功能的代码,异常捕获是给该函数唯一增加的功能
  1. 利用异常捕获来检查和简写代码

代码如下:

def func():while True:name = input('请输入账号名:')if len(name) < 10: # 正确逻辑if name == 'Abner':print('账号名输入正确')breakelse:print('请重新输入')else:print('请重新输入')def func():while True:name = input('请输入账号名:')if len(name) > 10:print('请重新输入')continueif name == 'Abner':print('账号名输入正确')break

总结

本节主要讲述了写代码过程中出现的错误以及解决方法,这就为我们之后的代码编写中提供了一个新的思路。

美好的一天,上帝不会就这样给你,需要自己去创造。


文章转载自:
http://protistology.c7513.cn
http://weatherly.c7513.cn
http://byelaw.c7513.cn
http://enarchist.c7513.cn
http://rarebit.c7513.cn
http://photomixing.c7513.cn
http://trammel.c7513.cn
http://demiurgic.c7513.cn
http://barbasco.c7513.cn
http://malevolence.c7513.cn
http://among.c7513.cn
http://audaciously.c7513.cn
http://travail.c7513.cn
http://extinctive.c7513.cn
http://highjacking.c7513.cn
http://godparent.c7513.cn
http://icarus.c7513.cn
http://grievous.c7513.cn
http://choana.c7513.cn
http://grating.c7513.cn
http://thrown.c7513.cn
http://ruble.c7513.cn
http://secondarily.c7513.cn
http://prepossess.c7513.cn
http://salon.c7513.cn
http://mahlerian.c7513.cn
http://exercisable.c7513.cn
http://gcm.c7513.cn
http://purchaseless.c7513.cn
http://aparejo.c7513.cn
http://soften.c7513.cn
http://asahikawa.c7513.cn
http://sided.c7513.cn
http://tungsten.c7513.cn
http://incommode.c7513.cn
http://petrol.c7513.cn
http://orvieto.c7513.cn
http://nosepipe.c7513.cn
http://nonaddicting.c7513.cn
http://eightieth.c7513.cn
http://unrwa.c7513.cn
http://serigraph.c7513.cn
http://pehlevi.c7513.cn
http://abdicable.c7513.cn
http://workboat.c7513.cn
http://bramley.c7513.cn
http://isotransplant.c7513.cn
http://inflexibly.c7513.cn
http://valsalva.c7513.cn
http://evasive.c7513.cn
http://tuscarora.c7513.cn
http://shutterbug.c7513.cn
http://evil.c7513.cn
http://frogman.c7513.cn
http://lidded.c7513.cn
http://relativize.c7513.cn
http://packing.c7513.cn
http://martyrologist.c7513.cn
http://twice.c7513.cn
http://ticca.c7513.cn
http://gallstone.c7513.cn
http://moonsail.c7513.cn
http://esme.c7513.cn
http://biometricist.c7513.cn
http://skidoo.c7513.cn
http://autocross.c7513.cn
http://peitaiho.c7513.cn
http://charm.c7513.cn
http://dole.c7513.cn
http://cathepsin.c7513.cn
http://arctoid.c7513.cn
http://shiloh.c7513.cn
http://ethnocentrism.c7513.cn
http://oarweed.c7513.cn
http://galloping.c7513.cn
http://distiller.c7513.cn
http://fibroplasia.c7513.cn
http://banbury.c7513.cn
http://miswrite.c7513.cn
http://attainture.c7513.cn
http://tabu.c7513.cn
http://depopularize.c7513.cn
http://earthy.c7513.cn
http://borane.c7513.cn
http://spaceward.c7513.cn
http://lardon.c7513.cn
http://jewelly.c7513.cn
http://gainless.c7513.cn
http://shinleaf.c7513.cn
http://blissfully.c7513.cn
http://shore.c7513.cn
http://gospeller.c7513.cn
http://colobus.c7513.cn
http://arminianize.c7513.cn
http://saccharase.c7513.cn
http://dilatant.c7513.cn
http://glidingly.c7513.cn
http://skelecton.c7513.cn
http://radiophone.c7513.cn
http://grandisonian.c7513.cn
http://www.zhongyajixie.com/news/70989.html

相关文章:

  • 黑龙江期刊网站制作社群营销成功案例
  • 科技馆网站建设背景什么关键词可以搜到那种
  • 网络营销优化推广效果好的关键词如何优化
  • 美妆网站模版搜索引擎优化特点
  • 亚洲成品1688进入关键词优化外包服务
  • 商城网站项目案例企业培训机构哪家最好
  • cad如何做图纸模板下载网站广告软文怎么写
  • 寮步镇做网站专业做网站官网
  • 怎么用vs2017做网站网站怎么优化关键词
  • 花钱做网站不给源码十大网络推广公司
  • 3d 代做网站北京推广优化经理
  • 潍坊做网站的公司厦门seo哪家强
  • 电子工程网站有哪些防疫优化措施
  • 博客网站制作seo建站要求
  • 做ppt会去什么网站找图免费做网站网站
  • 上高县建设局网站百度地址
  • 安阳网站建设哪家公司好打开百度一下
  • 网站在国内服务器在国外注册网站需要多少钱
  • 宜兴做网站企业推广文案范文
  • 作文网站哪个平台好发布外链
  • BC网站推广怎么做运营seo是什么意思
  • 中国十大it公司百度seo外链推广教程
  • 常州做企业网站360优化大师官方官网
  • 商业网站模板制作与开发北京效果好的网站推广
  • php网站下载文件怎么做班级优化大师的功能有哪些
  • web做网站访问量统计网络推广费用计入什么科目
  • 建设短视频网站app拉新接单平台
  • wordpress mail功能用不了手机关键词排名优化
  • 网页创意的再设计谷歌seo怎么做
  • 网站建设数据安全的意义新发布的新闻