Is it possible to catch several types of exceptions and then print the exact type that occurred?

This is the closest answer that I could find when searching for a few exceptions (the correct way), when, after a wonderful silence, the interpreter resolved the following fragment:

try: x = "5" + 3 except NameError and TypeError as e: print e 

docs will also provide this snippet, but nothing like the first:

 ... except (RuntimeError, TypeError, NameError): ... pass 

So, it would be nice to have a second opinion, but my question really comes down to the following:

  • How can I not only print the message, but insert the exact type of error at the beginning of the print statement. For example, I would prefer the first fragment to respond by typing this instead: TypeError: cannot concatenate 'str' and 'int' objects

It seems to me that this is possibly impossible or simple, given that the interpreter only lists args and message as members of NameError , but perhaps this is simply incomplete.

I tried this myself, but it no longer rules out errors (maybe I'm wrong):

 try: x = "5" + 3 except (NameError, TypeError) as e: if isinstance(e, NameError): print "NameError: " + e elif isinstance(e, TypeError): print "TypeError: " + e else: print e 
+4
source share
4 answers

Your problem is that you are trying to concatenate a string with an exception like this

 >>> 'foo' + TypeError Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: cannot concatenate 'str' and 'type' objects 

The easiest way:

 >>> 'foo' + str(TypeError) "foo<type 'exceptions.TypeError'>" >>> 
+1
source

You can catch errors individually. See this link for more examples: http://docs.python.org/2/tutorial/errors.html#handling-exceptions

Something like the following:

 try: x = "5" + 3 except TypeError as e: print "This is a TypeError" print e except NameError as e: print "This is a NameError" print e 
+8
source

Others answered your second question, so I will be the first to do:

 except NameError and TypeError as e: 

exactly the same as:

 except TypeError as e: 

It will not catch NameError . What for? Since NameError is true, therefore and evaluates and returns its second argument, TypeError . Try:

 print NameError and TypeError 

Yes, exceptions in the except clause are evaluated at runtime, like any other expression. In fact, Python does not even require them to be exceptions (however, if they are not, they won’t understand anything anymore because raise requires exceptions ... well, you can use strings, but they are deprecated).

For this reason, you must provide a tuple of exceptions to catch:

 except (TypeError, ValueError) as e: 

Other forms may be accepted by Python as a valid syntax, but probably will not work as you might expect.

+8
source

Since minitech has already shown you the best way to do what you want ( print type(e).__name__ ), I’ll just tell you what you did wrong.

If e = NameError("BAD, BAD, BAD!") , Then print e gives BAD, BAD, BAD! .

However, "My string" + e invalid because e not a string. Instead, use print "My string", e , print "My string" + str(e) or print "My string{}".format(e) .

+2
source

All Articles