I call an external program and raise an error when it fails. The problem is I can't catch my custom exception.
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'
In this case, python prints Oops, something went wrong: [the sderr message] with a stack-trace.