Error Reraising
Contents |
home cookingPicturesA Walk on the Pequest FillAutumn in the GapBroken Pipe(dreams)Calno to Wallpack CenterChesapeake Cruise June 2008FJ CruisingIron Road to ChesterMillbrook Village and Mountain RoadOBX AdventureOld Dingmans and Sand Pond RoadsThe Depue and Kinney FarmsThe Lehigh and Hudson python reraise error RRThe Orchard Trail and Ridge RoadThe Paulins Kill ViaductThe Vancampen Burial Ground
Ruby Reraise Error
April 30, 2014November 14, 2014 Re-raising exceptions in Python I often run into situations where an error occurs, an exception python six reraise is caught, and some action is taken before the exception is re-raised so that higher levels of the program can deal with it. Exactly how the exception is re-raised can have
Python 3 Re Raise Exception
a big impact on how much information is available to someone trying to debug a problem. I got this wrong as a newbie, and I've seen even experienced python programmers get tripped up by these subtleties so I thought I would try to post a clear example. Consider the following program: def bar(): raise Exception("Oops!") def foo(): bar() def dinkum(): foo() def fair(): try: python raise from dinkum() except Exception as e: print "Something failed!" raise e fair() As you can see I've created a few methods in order to have a stack trace to look at, and raised an exception in the last one to be called, foo(). That exception is caught in fair() and some information is printed before the exception is re-raised by the statement "raise e." This syntax looks pretty reasonable, so let's run it and see what happens: mark@viking:~/workspace$ python raise_test1.py Something failed! Traceback (most recent call last): File "raise_test.py", line 21, in fair() File "raise_test.py", line 19, in fair raise e Exception: Oops! Hmm. Looks like we're missing some useful information. The exception was raised on line 6 but the stack trace begins on line 19. Lets change the program slightly and run it again: def bar(): raise Exception("Oops!") # ... snip for brevity ... def fair(): try: dinkum() except Exception as e: print "Something failed!" raise fair() The only change here is that now we're simply using "raise" rather than re-raising the exception by name with "raise e." The output is quite different: mark@viking:~/workspace$ python raise_test2.py Something failed! Traceback
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring
Python Re Raise Exception With Message
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question
Python Raise Exception With Traceback
x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; python raise different exception it only takes a minute: Sign up Ruby reraising exception w/ additional string argument up vote 13 down vote favorite 4 Here is the situation. I want all exceptions in doStuff() to bubble up through the code so that http://www.markbetz.net/2014/04/30/re-raising-exceptions-in-python/ they are handled at a higher level. I'd also like to record how often any exceptions are happening in doStuff() at a higher level as well, and am currently doing this: begin doStuff() rescue Exception => raise e, "specific error to log in a db" doStuff code throw dozens of exceptions, and I want to capture each of these events to put in the db. There is a doStuff2(), which also can throw identical instructions, and I want to http://stackoverflow.com/questions/7250952/ruby-reraising-exception-w-additional-string-argument know which function they came from. Adding the additional string, seems to change the exception itself, and I lose all the nice formatting and trace information that the original exception had. Any suggestions on how I can reraise the original exception, but also keep track of all the exceptions that are occurring within doStuff()? ruby exception share|improve this question asked Aug 30 '11 at 23:32 Crazy Dan 11 10214 add a comment| 3 Answers 3 active oldest votes up vote 40 down vote If you call raise without passing any argument, Ruby will re-raise the last exception. begin doStuff() rescue => e log_exception(e) raise # This will re-raise the last exception. end As a side note, I'd like to give you some suggestions about Ruby best practices. doStuff() method doesn't follow Ruby naming conventions. You should use underscore for methods. Please use do_stuff. Also, no need to use () if there are no arguments. Never rescue Exception, instead rescue the most low-level class you need to rescue. In most cases, you might want to rescue all kind of StandardError or RuntimeError. In fact, if you use rescue without passing the error type, Ruby will automatically rescue any subclass of StandardError. Exception class is very low level and it will catch syntax errors as well, and several other compiler issues. You might want to let the application crash in this case so that you are not de
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site http://stackoverflow.com/questions/4825234/exception-traceback-is-hidden-if-not-re-raised-immediately About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Exception traceback is hidden if raise exception not re-raised immediately up vote 38 down vote favorite 6 I've got a piece of code similar to this: import sys def func1(): func2() def func2(): raise Exception('test error') def main(): err = None try: func1() except: err = sys.exc_info()[1] pass # some extra processing, involving checking err details (if err is not None) # need to re-raise err so caller can do its own re raise exception handling if err: raise err if __name__ == '__main__': main() When func2 raises an exception I receive the following traceback: Traceback (most recent call last): File "err_test.py", line 25, in