Python Exception Handling Difference Between Python 3 And 2

Python try catch exception statement is used to handle exceptions in python code, but there are some little differences of python exception handling between python 3 and python 2. Below example will show you the differences, let’s look at it.

Handle Exception In Python 2

try:
    raise
except Exception, e:
    print (e)
    return false

Handle Exception In Python 3

try:
    raise
except Exception as e:
    print (e)
    return false

The key difference is: Python 3 has an as keyword when catch exception, please pay attention to it.

Common Exception List

  1. Exception : The base class for all exceptions.
  2. AttributeError : Raised when attribute assignment fails.
  3. IOError : Raised when attempting to open a file that does not exist.
  4. IndexError : Raised when using an index that does not exist in the sequence.
  5. KeyError : Thrown when using a key that the map does not contain.
  6. NameError : Raised when the name (variable) cannot be found.
  7. SyntaxError : Raised when the code uses wrong syntax.
  8. TypeError : Thrown when use wrong python type objct.
  9. ValueError : Thrown when python object has an inappropriate value.
  10. ZeroDivisionError : Raised when the second parameter of a division operation is 0.
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Clicky
0
Would love your thoughts, please comment.x
()
x