Python Not Catching Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
Python Catch All Exceptions
and policies of this site About Us Learn more about Stack Overflow python exception message the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation syntax for generic except clause in python 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
Python Exception Stack Trace
only takes a minute: Sign up Python not catching exception up vote 1 down vote favorite For some reason my code is not catching an exception when I throw it. I have def trim_rad(rad): ... if not modrad.shape[0]: raise IndexError("Couldn't find main chunk") return modrad, thetas Then later I call that function: try: modrad, thetas = trim_rad(rad) except IndexError("Couldn't find main
Python Print Exception
chunk"): return 0 Yet I still get a traceback with that exception. What am I doing wrong? python share|improve this question asked Aug 15 '13 at 18:17 ari 654721 5 You're only supposed to put the type name there, not an instance of the type. If you want to further narrow it down to handle only a special IndexError that you will be throwing and leave regular ole IndexErrors alone, create a narrower exception type that inherits from IndexError and raise/catch that. –Asad Saeeduddin Aug 15 '13 at 18:20 1 I'm surprised this doesn't TypeError. I thought I tried except 1: before and got a TypeError, but trying it now, it just passes through. –user2357112 Aug 15 '13 at 18:23 @user2357112 Evidently it does cause a TypeError in Python 3: ideone.com/yy4XAy. You're right though, it doesn't matter in 2.x –Asad Saeeduddin Aug 15 '13 at 18:28 @Asad It's weird that there wasn't any warning that I was doing it wrong. So if I want to catch that specific error and not other IndexErrors
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind of python custom exception complaint you get while you are still learning Python: >>> while True print 'Hello world'
Python Try Without Except
File "
to using except: without specifying any exception class at all, meaning that it will catch all exceptions. Generally it's better to only catch the exceptions you are expecting, e.g., KeyError http://www.ianbicking.org/good-catch-all-exceptions.html or (IOError, OSError). I don't disagree with him, but there are a number of cases where this kind of exception handling is good, though often left out. The most obvious case: it's not bad at https://www.dotnetperls.com/error-python all to catch all exceptions if you re-raise them. In Python you can re-raise the last exception by using raise without any arguments. Basically you are saying if (and only if) something goes wrong, python exception do this, but still signal that something went wrong. This case shouldn't be confused with finally:, which indicates code that should always be called, error or no error. An example of except:: try: cursor.execute("INSERT INTO table VALUES (%s, %s)", (a, b)) obj.setValues(a, b) except: cursor.rollback() raise And one for finally:: conn = pscyopg.connect('dbname=testdb') try: run_transaction(conn) finally: conn.close() But there's other cases. Anytime you won't be there to babysit a process, python not catching you need to handle unexpected exceptions. For a command-line utility, you can read the exception when it occurs, no big deal. But for a long-running or batch process you need to intelligently deal with exceptions. Also for processes that are run by non-developers: you want to capture the error information so a developer can look at it, and then try to keep going if you can. In these cases you usually want to be careful about putting in except: blocks like the cursor.rollback() example above -- these attempt to clean things up when something unexpected goes wrong, so that the process isn't left in an inconsistent state. Then at some fairly high level you actually handle (and don't re-raise) the exception. Most "frameworks" do this for you -- e.g., Tkinter, Webware, Zope, cgitb, etc. If you don't have a framework, you have to do it yourself. Here's a really simple handler that uses the traceback module: def catch_exceptions(func, *args, **kw): try: # We could return the result of the function, but it's # a bad idea to make use of that return value since it's # undefined when there's an exception; it's better for the # function to set a flag or write to some persi