Python Multiple Error Catching
Contents |
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 python exception class Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Python Exception Message
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, python exception stack trace just like you, helping each other. Join them; it only takes a minute: Sign up Catch multiple exceptions in one line (except block) up vote 1223 down vote favorite 171 I know that I can do: try:
Python Custom Exception
# do something that may fail except: # do this if ANYTHING goes wrong I can also do this: try: # do something that may fail except IDontLikeYourFaceException: # put on makeup or smile except YouAreTooShortException: # stand on a ladder But if I want to do the same thing inside two different exceptions, the best I can think of right now is to do this: try: # do something that may fail except python print exception IDontLikeYouException: # say please except YouAreBeingMeanException: # say please Is there any way that I can do something like this (since the action to take in both exceptions is to say please): try: # do something that may fail except IDontLikeYouException, YouAreBeingMeanException: # say please Now this really won't work, as it matches the syntax for: try: # do something that may fail except Exception, e: # say please So, my effort to catch the two distinct exceptions doesn't exactly come through. Is there a way to do this? python exception exception-handling share|improve this question edited Sep 25 at 17:19 Mark Amery 24.2k12119151 asked Jun 24 '11 at 15:55 inspectorG4dget 44.9k1272134 add a comment| 4 Answers 4 active oldest votes up vote 1774 down vote accepted From https://docs.python.org/2/tutorial/errors.html#handling-exceptions: "An except clause may name multiple exceptions as a parenthesized tuple, for example": except (IDontLikeYouException, YouAreBeingMeanException) as e: pass Separating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now deprecated and does not work in Python 3; now you should be using as. share|improve this answer edited Sep 25 at 17:19 Mark Amery 24.2k12119151 answered Jun 24 '11 at 15:56 bernie 74.1k9124138 184 the 'as e:' is optional; if you don't want a reference to the exception object, you can le
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
Syntax For Generic Except Clause In Python
ZeroDivisionError: 5 print "divide by zero" If you wanted to examine the exception
Python Catch Multiple Exceptions
from code, you could have: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError as python try without except e: 5 z = e # representation: " Error: %s
to Program in Python Python Programming Special Python subjects / courses Tcl • Learning to Program in Tcl Tcl Programming Special Tcl, Expect, Tk subjects http://www.wellho.net/resources/ex.php4?item=y109/pushit.py / courses Ruby • Learning to Program in Ruby Ruby Programming Special Ruby, Rails subjects / courses Lua • Learning to Program in Lua Lua Programming Special Lua subjects / courses C & C++ • Learning to Program in C and C++ C and C++ Programming Other C and C++ subjects / courses Tomcat Deploying Apache / Tomcat Other python exception Tomcat and Linux subjects Search for: Print friendly page Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact • This site uses cookies - see [here] for details.• If you proceed, we will take that as your consent to accept cookies Handling multiple exceptions types Exceptions example from a Well House Consultants training course More on python multiple error Exceptions [link] Source code: pushit.py Module: Y109 #!/usr/local/bin/pythonprint "Selective trapping of user input errors"first = 1second = 2third = 3while 1:try:extra = input("What is the extra value? ")breakexcept EOFError:print " [EOF]\nNo data available. Default assumed"extra = 4breakexcept (NameError,SyntaxError):print "Please don't do that. Please enter a number"except StandardError, e:print "Strange input!"print "Exception type",e.__class__.__name__print "Please try again"total = first+second+third+extraprint total Learn about this subject This example is covered on the following public courses: * Learning to program in Python* Python Programming* Intermediate Python and is fully described on the course or in your printed course manual Books covering this topic Yes. We have over 700 books in our library. Books covering Python are listed here and when you've selected a relevant book we'll link you on to Amazon to order. Other Examples This example comes from our "Exceptions" training module. You'll find a description of the topic and some other closely related examples on the "Exceptions" module index page. Full description of the source code You can learn more about this example on the training courses listed on this