import sys import twisted.internet.defer import twisted.internet.reactor import twisted.python.log print "Twisted version", twisted.version print "Python version", sys.version class ExceptionToRaise(Exception): pass exception_to_raise = ExceptionToRaise() @twisted.internet.defer.inlineCallbacks def icb_error_maker(): """ Raises an exception implemented as an inline inlineCallbacks """ raise exception_to_raise def pod_error_maker(): """ Raises an exception implemented as a plain old deferred """ deferred = twisted.internet.defer.Deferred() deferred.errback(exception_to_raise) return deferred #ERROR_MAKER_TO_USE = icb_error_maker ERROR_MAKER_TO_USE = pod_error_maker @twisted.internet.defer.inlineCallbacks def intermediate_function3(): print 'intermediate_function3 1' try: yield ERROR_MAKER_TO_USE() except BaseException as exception: print 'intermediate_function3 exception', repr(exception) raise print 'intermediate_function3 2' @twisted.internet.defer.inlineCallbacks def intermediate_function2(): print 'intermediate_function2 1' try: yield intermediate_function3() except BaseException as exception: print 'intermediate_function2 exception', repr(exception) raise print 'intermediate_function2 2' @twisted.internet.defer.inlineCallbacks def intermediate_function1(): print 'intermediate_function1 1' try: yield intermediate_function2() except BaseException as exception: print 'intermediate_function1 exception', repr(exception) raise print 'intermediate_function1 2' def run(): d = intermediate_function1() d.addErrback(twisted.python.log.err) d.addBoth(lambda n: twisted.internet.reactor.stop()) twisted.internet.reactor.callWhenRunning(run) twisted.internet.reactor.run()