Created
December 29, 2012 03:16
-
-
Save bcho/4404326 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding: utf-8 | |
import sys | |
class CustomException(Exception): | |
'''Wrap arbitray pending exception, if any, in addition to other info.''' | |
def __init__(self, *args): | |
super(CustomException, self).__init__(*args) | |
self.wrapped_exc = sys.exc_info() | |
class AlwaysWrongException(Exception): | |
'''Always do it wrong.''' | |
def call_wrapped(callable, *args, **kwargs): | |
try: | |
return callable(*args, **kwargs) | |
except: | |
raise (CustomException, "Wrapped function propagated exception.") | |
def always_wrong(*args, **kwargs): | |
raise AlwaysWrongException | |
if __name__ == '__main__': | |
try: | |
call_wrapped(always_wrong) | |
except CustomException, e: | |
print e.wrapped_exc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment