0#1#2 异常处理

1抛出异常

raise Exception('xxxxxx')

2抓捕异常

try:
  pass
except Exception as err:#保存在err的变量之中
  print('xxx'+str(err))
finally:
  pass

3取得反向跟踪的字符串

import traceback
try:
    raise Exception('Error')
except:
    errorFile = open(path, 'w')
    errorFile.write(traceback.format_exc())
    errorFile.close()
    print('The traceback info was written to errorInfo.txt')

4断言

assert ...

assert 'red' in stoplight.values(), 'Neither light is red!' + str(stoplight)

如何禁用?
$ python -O err.py


5日志

日志是个好东西

举个栗子:
如何调用日志?

import logging
import logging.basicConfig(filename = Path, 
                           level = logging.DEBUG, 
                           format = '%(asctime)s - %(levelname)s - %(message)s'
)
#前面是放在之前的
#logging.disable(logging.CRITICAL)
logging.debug('Start of xxxxx')
logging.info('xxxx')
logging.warning('xxxx')
logging.error('xxx')
logging.critical('xxxxx')

注意前面的filename,可以存入一个文档里,然后边看文档,边打代码。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容