我如何使用堆栈跟踪来告诉我应该为 Python 的 try/except 使用哪个异常
How do I use stack trace to tell me which Exception I should use for Python's try/except
我想编写一个 Try/Except 块来捕获导致此堆栈跟踪的 特定 错误:
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/wrappers/local_cli_wrapper.py", line 292, in _prep_cli_for_run_start
self._run_cli = ui_factory.get_ui(self._ui_type)
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/ui_factory.py", line 61, in get_ui
return curses_ui.CursesUI(on_ui_exit=on_ui_exit, config=config)
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 289, in __init__
self._screen_init()
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 404, in _screen_init
self._screen_color_init()
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 409, in _screen_color_init
curses.use_default_colors()
_curses.error: use_default_colors() returned ERR
但无法弄清楚如何确定正确的异常是什么。
我写了以下内容 try/except 以获取更多信息:
try:
... call to procedure that generates error ...
except Exception,e:
print("type is:", e.__class__.__name__)
import sys
print(sys.exc_info())
我得到的结果是:
type is: error
(<class '_curses.error'>, error('use_default_colors() returned ERR',), <traceback object at 0x7fdec55abdd0>)
> /home/me/Projects/kerasECOC/net_manager.py(164)init_model_architecture()
但是,当我尝试
Except error,e:
我收到以下错误消息:
File "/home/me/Projects/kerasECOC/net_manager.py", line 157, in init_model_architecture
except error,e:
NameError: global name 'error' is not defined
那么,我如何确定要标记哪个特定异常?
因为回溯表明你应该使用 curses.error
:
import curses
try:
...
except curses.error as err:
print(err)
您可以检查 curses.error.mro()
的基数 类,您也可以除外:
>>> curses.error.mro()
[<class '_curses.error'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>]
但是它不继承自 concrete exceptions 之一。
我想编写一个 Try/Except 块来捕获导致此堆栈跟踪的 特定 错误:
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/wrappers/local_cli_wrapper.py", line 292, in _prep_cli_for_run_start
self._run_cli = ui_factory.get_ui(self._ui_type)
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/ui_factory.py", line 61, in get_ui
return curses_ui.CursesUI(on_ui_exit=on_ui_exit, config=config)
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 289, in __init__
self._screen_init()
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 404, in _screen_init
self._screen_color_init()
File "/home/me/anaconda2/envs/deepnn/lib/python2.7/site-packages/tensorflow/python/debug/cli/curses_ui.py", line 409, in _screen_color_init
curses.use_default_colors()
_curses.error: use_default_colors() returned ERR
但无法弄清楚如何确定正确的异常是什么。
我写了以下内容 try/except 以获取更多信息:
try:
... call to procedure that generates error ...
except Exception,e:
print("type is:", e.__class__.__name__)
import sys
print(sys.exc_info())
我得到的结果是:
type is: error
(<class '_curses.error'>, error('use_default_colors() returned ERR',), <traceback object at 0x7fdec55abdd0>)
> /home/me/Projects/kerasECOC/net_manager.py(164)init_model_architecture()
但是,当我尝试
Except error,e:
我收到以下错误消息:
File "/home/me/Projects/kerasECOC/net_manager.py", line 157, in init_model_architecture
except error,e:
NameError: global name 'error' is not defined
那么,我如何确定要标记哪个特定异常?
因为回溯表明你应该使用 curses.error
:
import curses
try:
...
except curses.error as err:
print(err)
您可以检查 curses.error.mro()
的基数 类,您也可以除外:
>>> curses.error.mro()
[<class '_curses.error'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>]
但是它不继承自 concrete exceptions 之一。