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日志
日志是個好東西
舉個栗子:
如何調(diào)用日志峻汉?
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拴事,可以存入一個文檔里供置,然后邊看文檔,邊打代碼剂习。