【问题标题】:Can't catch my raise exception python无法捕获我的引发异常 python
【发布时间】:2015-09-29 13:03:23
【问题描述】:

我调用外部程序并在失败时引发错误。问题是我无法捕获我的自定义异常。

from subprocess import Popen, PIPE

class MyCustomError(Exception):
    def __init__(self, value): self.value = value
    def __str__(self): return repr(self.value)

def call():
    p = Popen(some_command, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()
    if stderr is not '':
        raise MyCustomError('Oops, something went wrong: ' + stderr)

try:
    call()
except MyCustomError:
    print 'This message is never displayed'

在这种情况下,python 打印 糟糕,出了点问题:[sderr 消息] 带有堆栈跟踪。

【问题讨论】:

  • 代码按照您对我的期望工作。我注意到self 没有在if 行中定义。另外,当子类化时,你通常也想调用父类的__init__
  • 您需要提供minimal reproducible example
  • 您正在捕获异常,您的代码中没有错误,请添加堆栈跟踪。问题是发生了另一个您没有发现的异常。
  • 旁注:不要这样做if stderr is not '':;这依赖于巧合的对象身份测试,但不能保证。因为你知道它是一个str,而空的str 是假的(而所有其他都是真的),你可以用if stderr: 更简单/更有效地测试它。这会中断的示例:如果您迁移到 Python 3,则默认情况下来自 communicate 的返回是 bytes,而不是 strb'' 将是一个无错误的完成,在 if stderr: 下会正常运行,但在 if stderr is not '': 下会正常运行。
  • 单独的旁注:您的自定义错误正在执行 Exception 将为您执行的操作(您只是给传递了错误名称的参数)。只需使用class MyCustomError(Exception): pass,它就可以正常工作(并且可以通过使用公共变量名与其他Exceptions 正确互操作);不需要多余的 __init____str__ 定义。

标签: python exception raise


【解决方案1】:

试试这个:

from subprocess import Popen, PIPE

class MyCustomError(Exception):
    def __init__(self, value): self.value = value
    def __str__(self): return repr(self.value)

def call():
    p = Popen(['ls', '-la'], stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate()
    if self.stderr is not '':
        raise MyCustomError('Oops, something went wrong: ' + stderr)

try:
    call()
except MyCustomError:
    print 'This message is never displayed'
except Exception, e:
    print 'This message should display when your custom error does not happen'
    print 'Exception details', type(e), e.message

看一下异常类型(用type(e)表​​示)值的类型。看起来这是一个您需要捕获的异常...

希望对你有帮助,

【讨论】:

  • Exception, e的第二条消息没有显示,所以帮我找到了问题,谢谢。
猜你喜欢
  • 2013-02-26
  • 2013-12-14
  • 1970-01-01
  • 2014-12-24
  • 1970-01-01
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多