tamil
tamil

Reputation: 391

Python 2.7 exception handling syntax

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?

Upvotes: 16

Views: 43796

Answers (4)

abduljalil
abduljalil

Reputation: 153

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

Upvotes: 10

holroy
holroy

Reputation: 3127

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.

Upvotes: 10

Rolf of Saxony
Rolf of Saxony

Reputation: 22458

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

Upvotes: 4

Mateus Milanez
Mateus Milanez

Reputation: 125

Use the format

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

Upvotes: -1

Related Questions