Python Error Handling Finally
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶
Python Exception Class
Syntax errors, also known as parsing errors, are perhaps the most common python exception message kind of complaint you get while you are still learning Python: >>> while True print 'Hello world'
Python Exception Stack Trace
File "
Pages Local Site Map ------------------------ Rename Page Delete Page ------------------------ ------------------------ Remove Spam Revert to this revision ------------------------ SlideShow User Login Handling Exceptions The simplest way to handle exceptions is with a "try-except" block: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError: 5 print "divide
Syntax For Generic Except Clause In Python
by zero" If you wanted to examine the exception from code, you could have: python try without except 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError as e: 5 z = e # representation:
Python Try Except Else
" Error: %s
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 developers or posting http://stackoverflow.com/questions/1611561/can-i-get-the-exception-from-the-finally-block-in-python 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 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Can I get the exception from the finally block in python? up vote 23 down vote favorite 7 I have a try/finally clause in my script. Is it possible to get the exact error message from within python exception the finally clause? python error-handling share|improve this question edited May 31 '15 at 16:33 Mr_and_Mrs_D 9,1771070154 asked Oct 23 '09 at 5:37 Goutham 76651528 add a comment| 3 Answers 3 active oldest votes up vote 54 down vote accepted No, at finally time sys.exc_info is all-None, whether there has been an exception or not. Use: try: whatever except: here sys.exc_info is valid to re-raise the exception, use a bare `raise` else: here you know there was no exception finally: and here you python error handling can do exception-independent finalization share|improve this answer answered Oct 23 '09 at 5:43 Alex Martelli 480k898731150 Thanks. sys.exc_info (in an except clause) is what I need –Goutham Oct 23 '09 at 6:08 add a comment| up vote 7 down vote The finally block will be executed regardless of whether an exception was thrown or not, so as Josh points out, you very likely don't want to be handling it there. If you really do need the value of an exception that was raised, then you should catch the exception in an except block, and either handle it appropriately or re-raise it, and then use that value in the finally block -- with the expectation that it may never have been set, if there was no exception raised during execution. import sys exception_name = exception_value = None try: # do stuff except Exception, e: exception_name, exception_value = sys.exc_info()[:2] raise # or don't -- it's up to you finally: # do something with exception_name and exception_value # but remember that they might still be none share|improve this answer answered Oct 23 '09 at 5:56 Ian Clelland 24.2k55465 add a comment| up vote 2 down vote You'll want to do that in the except clause, not the finally. Refer to: http://www.doughellmann.com/articles/Python-Exception-Handling/ share|improve this answer answered Oct 23 '09 at 5:40 Josh 1,7341312 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up us