15

I am bit confused about the try exception usage in Python 2.7.

try:
    raise valueError("sample value error")
except Exception as e:
    print str(e)    

try:
    raise valueError("sample value error")
except Exception,exception:
    print str(exception)


try:
    raise valueError("sample value error")
except exception:
    print str(exception)


try:
    raise valueError("sample value error")
except Exception:
    print str(Exception) # it prints only the object reference

can some help me to understand the above usage?

2
  • If you want to catch an exception and do something with it, use your first example. The last example is when you don't actually care about capturing the error, you just want to know when an exception was thrown. The last example is more like control flow. The first example is more about seeing why a certain call failed. Sep 16, 2015 at 16:00
  • 1
    Please focus your question. You seem to be asking for tutoring somewhat more broad than StackOverflow's purpose. You should execute each of these examples, examine the output, and compare that with the descriptions from your textbook and on-line materials. If you still don't understand a specific result, post the example, the output, and a description of where you're confused. As a start, note that "Exception" is a very basic class, provided as part of the Python language; "exception" is a global variable.
    – Prune
    Sep 16, 2015 at 16:13

4 Answers 4

10

For Python 3 (also works in Python 2.7):

try:
      raise ValueError("sample value error")
except Exception as e:
      print(e)

For Python 2 (will not work in Python 3):

try:
      raise ValueError("sample value error")
except Exception, e:
      print e
9

Some concepts to help you understand the difference between the alternate variants of the except variants:

  • except Exception, e – This in an older variant, now deprecated, similar to except Exception as e
  • except Exception as e – Catch exceptions of the type Exception (or any subclass) and store them in the variable e for further processing, messaging or similar
  • except Exception – Catch exceptions of the type Exception (or any subclass), but ignore the value/information provided in the exception
  • except e – Gives me an compilation error, not sure if this related to python version, but if so, it should/would mean that you don't care about the type of exception but want to access the information in it
  • except – Catch any exception, and ignore the exception information

What to use, depends on many factors, but if you don't need the provided information in the exception there is no need to present the variable to catch this information.

Regarding which type of Exception to catch, take care to catch the accurate type of exceptions. If you are writing a general catch it all, it could be correct to use except Exception, but in the example case you've given I would opt for actually using except ValueError directly. This would allow for potentially other exceptions to be properly handled at another level of your code. The point is, don't catch exception you are not ready to handle.

If you want, you can read more on python 2.7 exception handling or available python 2.7 exception in the official documentation.

0
4

I use:

try:
    raise valueError("sample value error")
except Exception as e:
    print str(e) 

When I want to declare a specific error and

try:
    raise valueError("sample value error")
except:
    print "Something unexpected happened"

When I don't really care or except: pass , except: return etc

-1

Use the format

try:
    raise ValueError("sample value error")
except Exception, e:
    print e
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.