Re: saving an exception
Bryan wrote: i would like to save an exception and reraise it at a later time. something similar to this: exception = None def foo(): try: 1/0 except Exception, e: exception = e if exception: raise exception with the above code, i'm able to successfully raise the exception, but the line number of the exception is at the place of the explicit raise instead of the where the exception originally occurred. is there anyway to fix this? Sure: generate the stack trace when the real exception occurs. Check out sys.exc_info() and the traceback module. import sys import traceback exception = None def foo(): global exception try: 1/0 except Exception: # Build a new exception of the same type with the inner stack trace exctype = sys.exc_info()[0] exception = exctype('\nInner ' + traceback.format_exc().strip()) foo() if exception: raise exception # Output: Traceback (most recent call last): File foo.py, line 15, in module raise exception ZeroDivisionError: Inner Traceback (most recent call last): File foo.py, line 8, in foo 1/0 ZeroDivisionError: integer division or modulo by zero --Ben -- http://mail.python.org/mailman/listinfo/python-list
Re: saving an exception
Bryan wrote: hi, i would like to save an exception and reraise it at a later time. something similar to this: exception = None def foo(): try: 1/0 except Exception, e: exception = e if exception: raise exception i have a need to do this because in my example foo is a callback from c code which was originally called from python and i can't modify the c code. with the above code, i'm able to successfully raise the exception, but the line number of the exception is at the place of the explicit raise instead of the where the exception originally occurred. is there anyway to fix this? You can capture the trace at the time the original exception is raised, if that would help? I think the traceback module lets you do that. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC/Ltd http://www.holdenweb.com Skype: holdenweb http://holdenweb.blogspot.com Recent Ramblings http://del.icio.us/steve.holden -- http://mail.python.org/mailman/listinfo/python-list
Re: saving an exception
At Tuesday 3/10/2006 02:15, Bryan wrote: i would like to save an exception and reraise it at a later time. def foo(): Â Â try: Â Â Â Â 1/0 Â Â except Exception, e: Â Â Â Â exception = e if exception: raise exception with the above code, i'm able to successfully raise the exception, but the line number of the exception is at the place of the explicit raise instead of the where the exception originally occurred. Â is there anyway to fix this? The raise statement has 3 arguments, the third being the traceback (not used so much, except in cases like yours). You can get the values using sys.exc_info() (Don't store the traceback more than needed because it holds a reference to all previous stack frames...) Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
saving an exception
hi, i would like to save an exception and reraise it at a later time. something similar to this: exception = None def foo(): try: 1/0 except Exception, e: exception = e if exception: raise exception i have a need to do this because in my example foo is a callback from c code which was originally called from python and i can't modify the c code. with the above code, i'm able to successfully raise the exception, but the line number of the exception is at the place of the explicit raise instead of the where the exception originally occurred. is there anyway to fix this? thanks, bryan -- http://mail.python.org/mailman/listinfo/python-list