Error Handling Python 3.2
a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception classes from which it is derived). Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions listed below can be generated by the interpreter or built-in functions. Except where mentioned, they have an "associated value" indicating the detailed cause of the error. This may be a string or a tuple of several items of information (e.g., an error code and a string explaining the code). The associated value is usually passed as arguments to the exception class's constructor. User code can raise built-in exceptions. This can be used to test an exception handler or to report an error condition "just like" the situation in which the interpreter raises the same exception; but beware that there is nothing to prevent user code from raising an inappropriate error. The built-in exception classes can be subclassed to define new exceptions; programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException. More information on defining exceptions is available in the Python Tutorial under User-defined Exceptions. When raising (or re-raising) an exception in an except or finally clause __context__ is automatically set to the last exception caught; if the new exception is not handled the traceback that is eventually displayed will include the originating exception(s) and the final exception. When raising a new exception (rather than using a bare raise to re-raise the exception currently being handled), the implicit exception context can be supplemented with an explicit cause by using from with raise: raise new_exc from original_exc The expression following from must be an exception or None. It will be set as __cause__ on the raised exception. Setting __cause__ also implicitly sets the 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 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 try … except … as error in Python 2.5 - Python 3.x up vote 20 down vote favorite 6 I want https://docs.python.org/3/library/exceptions.html to keep & use the error value of an exception in both Python 2.5, 2.7 and 3.2. In Python 2.5 and 2.7 (but not 3.x), this works: try: print(10 * (1/0)) except ZeroDivisionError, error: # old skool print("Yep, error caught:", error) In Python 2.7 and 3.2 (but not in 2.5), this works: try: print(10 * (1/0)) except (ZeroDivisionError) as error: # 'as' is needed by Python 3 print("Yep, error caught:", error) Is there any http://stackoverflow.com/questions/11285313/try-except-as-error-in-python-2-5-python-3-x code for this purpose that works in both 2.5, 2.7 and 3.2? Thanks python exception try-catch share|improve this question edited Jul 1 '12 at 20:30 Lion 9,618135385 asked Jul 1 '12 at 20:17 user1492917 304139 Can you not upgrade your 2.5 code to at least 2.7 and preferably 3? –Ben Jul 1 '12 at 20:35 except (ZeroDivisionError) as error: works fine in python 2.7 ideone.com/KfHBs –Ashwini Chaudhary Jul 1 '12 at 20:35 2 Python 3 is expressly not backwards compatible. Why restrict yourself to a limited subset of syntax that both languages support? –Cameron Jul 1 '12 at 20:40 @Ben (and Cameron) The code is used on appliances (often with Python 2.5) and on Linux installation (with Python 3.x on the horizon). Therefore both 2.5 and 3.x should work .... If I must choose, it will be 2.x code; that will work on on all installations: appliances and Linux installations. –user1492917 Jul 1 '12 at 20:48 2 @AshwiniChaudhary Indeed. That's what I already said in my post. Python 2.7 looks like a fine hybrid: accepting both 2.x and 3.x style python code. It's about 2.5 versus 3.x ... –user1492917 Jul 1 '12 at 20:49 | show 1 more comment 1 Answer 1 active oldest votes up vote 30 down vote a motivate you to write clean, readable and efficient code in Python. Python has many built-in exceptionswhich forces your program to output an error when something in it goes wrong. http://www.programiz.com/python-programming/exception-handling Above_Image" data-ad-client="ca-pub-9007421361448641" data-ad-slot="8057468026" When these exceptions occur, it causes the current process to stop and passes it to the calling process until it is handled. If not handled, our program will crash. For example, if function A calls function B which in turn calls function C and an exception occurs in function C. If it is not handled in C, the exception passes to B and then to error handling A. If never handled, an error message is spit out and our program come to a sudden, unexpected halt. Catching Exceptions in Python In Python, exceptions can be handled using a try statement. A critical operation which can raise exception is placed inside the try clause and the code that handles exception is written in except clause. It is up to us, what operations we perform once we have error handling python caught the exception. Here is a simple example. # import module sys to get the type of exception import sys randomList = ['a', 0, 2] for entry in randomList: try: print("The entry is", entry) r = 1/int(entry) break except: print("Oops!",sys.exc_info()[0],"occured.") print("Next entry.") print() print("The reciprocal of",entry,"is",r) Output The entry is a Oops! be down. Please try the request again. Your cache administrator is webmaster. Generated Tue, 11 Oct 2016 16:05:01 GMT by s_ac15 (squid/3.5.20)