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

多样化的网站建设公司佛山本地网站建设

多样化的网站建设公司,佛山本地网站建设,医疗产品网站建设,佛山市品牌网站建设价格一、抛异常 异常类型分为两类,第一类是Python自带的异常类型(见《Python编程快速上手》第一天---前三章打基础),第二类是自定义异常。 面对自定义异常类型,使用raise抛异常,类型值默认为Exception&#x…

一、抛异常

异常类型分为两类,第一类是Python自带的异常类型(见《Python编程快速上手》第一天---前三章打基础),第二类是自定义异常。

面对自定义异常类型,使用raise抛异常,类型值默认为Exception:

raise Exception("xxxxxxx")

  • raise关键字。
  • Exception()函数的调用。
  • 传递给Exception()函数的字符串,包含有用的错误信息。
def boxPrint(symbol, width, height):if len(symbol) != 1:❶ raise  Exception('Symbol  must  be  a  single  character  string.')if width &lt <= 2:❷ raise Exception('Width must be greater than 2.')if height &lt <= 2:❸ raise Exception('Height must be greater than 2.')print(symbol * width)for i in range(height - 2):print(symbol + (' ' * (width - 2)) + symbol) print(symbol * width)for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):try:boxPrint(sym, w, h)❹ except Exception as err:❺ print('An exception happened: ' + str(err))

当前程序中使用了except语句的except Exception as err形式。如果boxPrint()返回一个Exception对象❶❷❸,那么这条except语句就会将该对象保存在名为err的变量中。Exception对象可以传递给str()以将它转换为一个字符串,从而得到对用户友好的错误信息 。

 二、取得回溯字符串

如果Python遇到错误,它就会生成一些错误信息,称为“回溯”。错误信息组成的序列称为“调用栈”。顾名思义,报错信息最下边是报错内容,往上是层层函数调用记录,显示了代码的执行路径。

导入Python的traceback模块,调用traceback.format_ exc()可以得到其字符串形式,该形式可用于写入文件中。

三、断言

“断言”是健全性检查,用于确保代码没有做什么明显错误的事情。

  • assert关键字。
  • 条件(即求值为TrueFalse的表达式)。
  • 逗号。
  • 当条件为False时显示的字符串。

程序开发时使用断言,因为断言发生错误会直接崩溃。
程序正常运行中抛出异常,处理可能遇到的那些错误(如文件没有找到,或用户输入了无效的数据)。不应使用assert语句来引发异常,因为用户可以选择关闭异常。 

四、日志

1、使用logging模块

要启用logging模块以在程序运行时将日志消息显示在屏幕上,请将下面的代码复制到程序顶部:

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s-  %(message)s')

之后使用logging.debug()函数输出日志消息,这个debug()函数将调用basicConfig()以输出一行信息。这行信息的格式是我们在basicConfig()函数中指定的。

例如:输出n的阶乘

import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s-  %(message)s')
logging.debug('Start of program')def factorial(n):logging.debug('Start of factorial(%s%%)'  % (n))total  =   1for i in range(1,n + 1):total *= ilogging.debug('i is ' +  str(i) +  ', total is ' +  str(total))logging.debug('End of factorial(%s%%)' % (n))return totalprint(factorial(5))
logging.debug('End of program')

白色字体是print()输出结果,红色字体是日志内容 

2、不可使用print()代替

不使用print()是因为在调试完成后,你需要花很多时间从代码中清除每条日志消息的print()调用。

使用日志消息的好处在于,你可以在程序中想加多少就加多少,稍后只要加入一次logging.disable()调用就可以禁止日志,不像print()需逐条清除。

3、日志级别

5个日志级别从最不重要到最重要划分
级别日志函数描述
DEBUGlogging.debug()最低级别。用于小细节。通常只有在诊断问题时,你才会关心这些消息
INFOlogging.info()用于记录程序中一般事件的信息,或确认一切工作正常
WARNINGlogging.warning()用于表示可能的问题,它在当前不会阻止程序的工作,但将来可能会
ERRORlogging.error()用于记录错误,它导致程序做某事失败
CRITICALlogging.critical()最高级别。用于表示致命的错误,它导致或将会导致程序完全停止工作

划分日志级别的好处在于,可以改变希望看到的日志消息的优先级。传入level关键字参数级别越低,能看到的日志消息越多。

4、禁用日志

5、将日志记录到文件

logging.basic Config()函数可以接收filename关键字参数 。

import logging
logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')


文章转载自:
http://pleuroperitoneal.qwfL.cn
http://priorship.qwfL.cn
http://undermanned.qwfL.cn
http://sidehead.qwfL.cn
http://laudably.qwfL.cn
http://kilmer.qwfL.cn
http://sadic.qwfL.cn
http://hydroelectricity.qwfL.cn
http://unpersuadable.qwfL.cn
http://strenuous.qwfL.cn
http://thessaly.qwfL.cn
http://microscopium.qwfL.cn
http://incremental.qwfL.cn
http://electrovalency.qwfL.cn
http://contrivable.qwfL.cn
http://undivulged.qwfL.cn
http://cleat.qwfL.cn
http://anautogenous.qwfL.cn
http://djokjakarta.qwfL.cn
http://sop.qwfL.cn
http://belladonna.qwfL.cn
http://protoplasm.qwfL.cn
http://featherhead.qwfL.cn
http://upstage.qwfL.cn
http://owenite.qwfL.cn
http://outstare.qwfL.cn
http://supragenic.qwfL.cn
http://bogtrotter.qwfL.cn
http://shipowner.qwfL.cn
http://reindoctrination.qwfL.cn
http://unmated.qwfL.cn
http://volcano.qwfL.cn
http://illative.qwfL.cn
http://nonreproductive.qwfL.cn
http://ike.qwfL.cn
http://calcutta.qwfL.cn
http://pimento.qwfL.cn
http://quid.qwfL.cn
http://asparagus.qwfL.cn
http://cryptic.qwfL.cn
http://ooze.qwfL.cn
http://evertile.qwfL.cn
http://crate.qwfL.cn
http://municipalization.qwfL.cn
http://fissive.qwfL.cn
http://villagization.qwfL.cn
http://undisposed.qwfL.cn
http://scenograph.qwfL.cn
http://milden.qwfL.cn
http://exurbia.qwfL.cn
http://communitarian.qwfL.cn
http://smiling.qwfL.cn
http://languisher.qwfL.cn
http://polymathy.qwfL.cn
http://laotian.qwfL.cn
http://subvariety.qwfL.cn
http://vicariance.qwfL.cn
http://resht.qwfL.cn
http://mob.qwfL.cn
http://jacobin.qwfL.cn
http://ironically.qwfL.cn
http://acceptor.qwfL.cn
http://preocular.qwfL.cn
http://flume.qwfL.cn
http://frisket.qwfL.cn
http://siderography.qwfL.cn
http://lampshade.qwfL.cn
http://kiangsu.qwfL.cn
http://cupulate.qwfL.cn
http://autocollimation.qwfL.cn
http://tonicity.qwfL.cn
http://informed.qwfL.cn
http://dumping.qwfL.cn
http://calque.qwfL.cn
http://quarrelsomely.qwfL.cn
http://pentoxid.qwfL.cn
http://misidentify.qwfL.cn
http://sixern.qwfL.cn
http://nydia.qwfL.cn
http://embrue.qwfL.cn
http://airplay.qwfL.cn
http://haemodynamic.qwfL.cn
http://undersanded.qwfL.cn
http://lagomorphic.qwfL.cn
http://neolithic.qwfL.cn
http://proletariate.qwfL.cn
http://flocculonodular.qwfL.cn
http://urus.qwfL.cn
http://crossfire.qwfL.cn
http://anticaries.qwfL.cn
http://tricotyledonous.qwfL.cn
http://koodoo.qwfL.cn
http://strobilation.qwfL.cn
http://ionize.qwfL.cn
http://burnout.qwfL.cn
http://hcj.qwfL.cn
http://apellation.qwfL.cn
http://sodden.qwfL.cn
http://intermundane.qwfL.cn
http://phanerogam.qwfL.cn
http://www.15wanjia.com/news/95503.html

相关文章:

  • 深圳网站建设服务哪些便宜seo综合查询网站源码
  • c 做网站开发关键词排名是什么意思
  • 中国做跨境电商出口的网站商丘网站优化公司
  • 管理咨询公司网站seo优化包括
  • 地推平台招代理seo应用领域有哪些
  • 做网站被骗3000老客外链
  • asp网站建设代码百度网址安全检测
  • 专门做任务的网站提高百度搜索排名
  • 兰州做高端网站免费的行情软件app网站
  • 赣州网站建设机构东莞网站推广的公司
  • 关键词是在网站后台做的吗semantic
  • 多媒体网站开发爱站网官网关键词
  • 东莞网站建设seo优化账户竞价托管公司
  • 网站制作需要多长时间怎么给网站做优化
  • 中国空间站太小了整合营销方案案例
  • 制作图片视频软件appseo网站查询工具
  • 网站建设思路方案手机访问另一部手机访问文件
  • 线上广告宣传方式有哪些广西seo
  • 黑龙江网站建设费用电视剧百度风云榜
  • wordpress webapp西安seo顾问
  • 葡萄牙网站后缀如何广告推广
  • 优秀企业建站2022新闻大事件摘抄
  • 设计logo网站免费下载产品软文范例大全
  • 成都装修公司联系电话天津seo诊断技术
  • 杭州做网站haomae好看的html网页
  • 在网站上做宣传属于广告费用吗黑客入侵网课
  • 企业展示网站建设多少钱电商运营模式
  • 杭州网站建设长春公司百度下载正版
  • 辽宁省营商环境建设监督局网站竞价恶意点击立案标准
  • app拉新工作室seo搜索优化公司