如果我执行错误的代码, python 原生的抛出异常能处理得非常优雅:
>>> print a
File "<stdin>", line 1
print a
^
SyntaxError: Missing parentheses in call to 'print'
自己写的代码如何才能这么优雅地抛出异常呢?
1
clino 2015-09-30 13:32:58 +08:00
我感觉你是想说打印出 traceback 吧?
|
2
chengzhoukun 2015-09-30 13:36:37 +08:00
http://blog.csdn.net/handsomekang/article/details/9373035
import traceback try: print a except Exception as e: traceback.print_exc() |
3
chengzhoukun 2015-09-30 13:40:17 +08:00
|
4
fengjianxinghun 2015-09-30 14:05:40 +08:00
抛异常和捕获是 2 个事吧。。。
|
5
DiffView 2015-10-01 00:55:05 +08:00
写个 log 模块比如 https://www.fancycoding.com/custom-python-log-module/
然后 try: momoda except Exception as e: LogC(self, traceback.format_exc()) 得到 2014-12-06 15:10:56,818 - C - CRITICAL - you can't momoda me |
6
deangl 2015-10-03 12:05:37 +08:00 via Android
logging.exception(e)
这个? |