Error Handling Python 2.5
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 How do I print Python 2.5 Exception arguments? up vote 2 down vote favorite Does python 2.5 allow you to pass exception arguments? try: raise Exception("argument here") except Exception: print Exception.args I've had no luck with the above code - I know this is how you do it in Python 2.7 - is this not in Python 2.5? exception-handling python-2.5 share|improve this question edited Oct 23 '10 at 3:53 asked Oct 23 '10 at 3:45 ehfeng 1,54632132 add a comment| 1 Answer 1 active oldest votes up vote 3 down vote accepted You aren't actually raising the exception, just creating it. Once you fix that, you also need to refer to the instance that gets raised, not just the Exception class: >>> try: ... raise Exception('foo', 23) ... except Exception, e: ... print e.args ... ('foo', 23) share|improve this answer edited Oct 23 '10 at 4:21 answered Oct 23 '10 at 3:51 Marcelo Cantos 117k21236290 I don't get that. I get:
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 Print info about exception in python 2.5? up vote 4 down vote favorite Python 2.5 won't let me use this syntax: try: code_that_raises_exception() except Exception as e: print e raise So http://stackoverflow.com/questions/4002374/how-do-i-print-python-2-5-exception-arguments how should I print information about an exception? Thanks EDIT: I'm writing a plugin for a program that includes kind of a pseudo python interpreter. It prints print statements but doesn't show exceptions at all. python exception exception-handling printing share|improve this question asked Sep 28 '10 at 0:14 Jeff 7471923 add a comment| 2 Answers 2 active oldest votes up vote 5 down vote accepted the 'as' keyword is a python 3 (introduced in 2.6) addition, you need http://stackoverflow.com/questions/3808812/print-info-about-exception-in-python-2-5 to use a comma: try: code_that_raises_exception() except Exception, e: print e raise share|improve this answer answered Sep 28 '10 at 0:19 Mike Axiak 7,74612038 thanks, works great. –Jeff Sep 28 '10 at 0:37 @Mike Axiak, what if you want to catch different exceptions which I'd usually do like ValueError as Verr, NameError as Nerr,...? –LarsVegas Aug 20 '12 at 7:35 except (ExceptionA, ExceptionB), e ? –djmccorrie Mar 8 at 13:51 add a comment| up vote 2 down vote try: codethatraises() except Exception, e: print e raise not as easy to read as the latest and greatest syntax, but identical semantics. share|improve this answer answered Sep 28 '10 at 0:17 Alex Martelli 477k898671149 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged python exception exception-handling printing or ask your own question. asked 6 years ago viewed 2278 times active 6 years ago Visit Chat Related 281How do you test that a Python function throws an exception?1266Catch multiple exceptions at once?603How to flush output of Python print?389Try/Except in Python: How do you p
Python 2.5 is scheduled for August 2006; PEP 356 describes the planned release schedule. The changes in Python 2.5 are an interesting mix of language https://docs.python.org/2/whatsnew/2.5.html and library improvements. The library enhancements will be more important to Python's user community, I think, because several widely-useful packages were added. New modules include ElementTree for XML processing (xml.etree), the http://www.blog.pythonlibrary.org/2012/09/12/python-101-exception-handling/ SQLite database module (sqlite), and the ctypes module for calling C functions. The language changes are of middling significance. Some pleasant new features were added, but most of them error handling aren't features that you'll use every day. Conditional expressions were finally added to the language using a novel syntax; see section PEP 308: Conditional Expressions. The new ‘with‘ statement will make writing cleanup code easier (section PEP 343: The ‘with' statement). Values can now be passed into generators (section PEP 342: New Generator Features). Imports are now visible as either absolute error handling python or relative (section PEP 328: Absolute and Relative Imports). Some corner cases of exception handling are handled better (section PEP 341: Unified try/except/finally). All these improvements are worthwhile, but they're improvements to one specific language feature or another; none of them are broad modifications to Python's semantics. As well as the language and library additions, other improvements and bugfixes were made throughout the source tree. A search through the SVN change logs finds there were 353 patches applied and 458 bugs fixed between Python 2.4 and 2.5. (Both figures are likely to be underestimates.) This article doesn't try to be a complete specification of the new features; instead changes are briefly introduced using helpful examples. For full details, you should always refer to the documentation for Python 2.5 at https://docs.python.org. If you want to understand the complete implementation and design rationale, refer to the PEP for a particular new feature. Comments, suggestions, and error reports for this document are welcome; please e-mail them to the author or open a bug in the Python bug tracker. PEP 308: Conditional Expressions¶ For a long time, people have
101Mike Python provides robust exception handing baked right into the language. Exception handing is something every programmer will need to learn. It allows the programmer to continue their program or gracefully terminate the application after an exception has occurred. Python uses a try/except/finally convention. We'll spend some time learning about standard exceptions, how to create a custom exception and how to get the exception information in case we need it for debugging. Basic Exception Handling To begin, it must be said that bare exceptions are usually NOT recommended! You will see them used though and I've used them myself from time to time. A bare except looks like this: try: print monkey except: print "This is an error message!" As you can see, this will catch ALL exceptions, but you don't really know anything about the exception which makes handling it pretty tricky. Because you don't know what exception was caught, you also can't print out a very relevant message to the user. On the other hand, there are times when I think it's just plain easier to use a bare except. One of my readers contacted me about this and said that database interactions are a good example. I've noticed that I seem to get a wide array of exceptions when dealing with databases too, so I would agree. I think when you reach more than 3 or 4 exception handlers in a row, it's better to just use a bare except and if you need the traceback, there are ways to get it (see the last section). Let's take a look at a few normal examples of exception handling. try: import spam except ImportError: print "Spam for eating, not importing!" try: print python except NameError, e: print e try: 1 / 0 except ZeroDivisionError: print "You can't divide by zero!" The first example catches an ImportError only which happens when you import something that Python cannot find. You'll see this if you try to import SQLAlchemy without actually having it installed or when you mis-spell a module or package name. One good use for catching ImportErrors is when you want to import an alternate module. Take the md5 module. It was deprecated in Python 2.5 so if you are writing code that supports 2