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,可以存入一个文档里,然后边看文档,边打代码。
网友评论