python - Python - 在异常中捕获异常?
问题描述
我定义了这个异常:
class ArgumentsException(Exception):
"""Exception that is raised when incorrect arguments are used."""
pass
现在我运行我的测试,我通过sh
包运行我的程序。并且当它引发这个预期的异常时,会sh
捕获该异常本身,然后重新引发他自己的异常。有没有办法让我检查我的原始异常是否以某种方式引发?
例如,当我运行此代码时(此代码预计会引发该异常):
sh.python3(
self.main_py_path,
self.live_cfg_path,
self.workflow_cfg_path)
我得到了这个例外:
Traceback (most recent call last):
File "/home/oerp/src/devops-tools/tests/test_main.py", line 153, in test_full_workflow_1
self.workflow_cfg_path)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 1427, in __call__
return RunningCommand(cmd, call_args, stdin, stdout, stderr)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 774, in __init__
self.wait()
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 792, in wait
self.handle_command_exit_code(exit_code)
File "/usr/local/lib/python3.5/dist-packages/sh.py", line 815, in handle_command_exit_code
raise exc
sh.ErrorReturnCode_1:
RAN: /usr/bin/python3 /home/oerp/src/devops-tools/main.py /home/oerp/src/devops-tools/tests/configs/__live__.py /home/oerp/src/devops-tools/tests/configs/__workflow__.py
STDOUT:
STDERR:
Traceback (most recent call last):
File "/home/oerp/src/devops-tools/main.py", line 204, in <module>
state = _get_state(args.state, ignore_state=args.ignore_state)
File "/home/oerp/src/devops-tools/main.py", line 68, in _get_state
"__state__.py file must be provided if --ignore-state flag "
exceptions.ArgumentsException: __state__.py file must be provided if --ignore-state flag is not used.
好吧,我可以做类似的事情:
self.assertTrue('ArgumentsException' in str(e.stderr))
但也许有更优雅的方式来检查我的异常?
解决方案
推荐阅读
- c# - 登录 ASP.NET 网站并刷新页面后 httpContext.Request 不包含 auth cookie
- javascript - Javascript / AngularJS - 按顺序将字符串转换为数字
- sql - 如何使用窗口滞后功能对具有多种传感器类型的数据进行分区
- webforms - UniSubroutine 异常
- python - 我应该在同一个端口上运行普通的烧瓶应用程序和套接字吗?
- spring-integration - 春天云流。将事件列表作为单个事件进行采购
- jakarta-ee - Java 项目集群以实现高可用性:如何从项目中使用的第三方框架中序列化类?
- mysql - MySQL 为什么将限制从 20 增加到 21 将执行时间从 1,4s 减少到 0,0067s
- javascript - 检测在 devtools 中所做的更改
- swift - 一个新分配的对象可以以前在 Swift 中使用过 ObjectIdentifier 吗?