如果我执行错误的代码, python 原生的抛出异常能处理得非常优雅:
>>> print a
File "<stdin>", line 1
print a
^
SyntaxError: Missing parentheses in call to 'print'
自己写的代码如何才能这么优雅地抛出异常呢?
1
clino 2015 年 9 月 30 日
我感觉你是想说打印出 traceback 吧?
|
2
chengzhoukun 2015 年 9 月 30 日
http://blog.csdn.net/handsomekang/article/details/9373035
import traceback try: print a except Exception as e: traceback.print_exc() |
3
chengzhoukun 2015 年 9 月 30 日
|
4
fengjianxinghun 2015 年 9 月 30 日
抛异常和捕获是 2 个事吧。。。
|
5
DiffView 2015 年 10 月 1 日
写个 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 月 3 日 via Android
logging.exception(e)
这个? |