2

In some exception handling code, I'd like to perform an emergency shutdown operation and then re-raise the exception. I am using an asynchronous programming model (Twisted), and I would like to re-raise the exception in the emergency shutdown's callback.

I'd like to do something like this:

except Exception as e:
    d = emergencyStop()
    d.addCallback(partial(raiseExn, e))

I could define my own raiseExn function:

def raiseExn(e):
    raise e

But it seems silly to add three or four lines to every place I want to use this code. I expect that the raise keyword probably calls a function somewhere in the Python standard library and it would be better to use that. But what is that function?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Dolan Murvihill
  • 345
  • 1
  • 3
  • 11

1 Answers1

0

You can use the generator.throw for this purpose. The example looks like

except Exception as e:
    d = emergencyStop()
    d.addCallback(lambda: (_ for _ in ()).throw(e)))
Chris.Q
  • 1,370
  • 16
  • 21