Updated Syntax for Exceptions
Python 3 will also streamline the syntax for exceptions, in handling as well as in raising. The existing idioms are either inconsistent and lead to incorrect code, or there are too many ways of doing the same thing. Both problems are corrected in Python 3.
In the past, the syntax to catch an exception and the exception argument/instance was as follows:
except ValueError, e:
To catch multiple exceptions with the same handler, you would use this:
except (ValueError, TypeError), e:
The required parentheses confused some users, who attempt this invalid version:
except ValueError, TypeError, e:
The new as keyword avoids confusion with the comma in the original syntax; however, the parentheses are still required when you're trying to catch more than one type of exception by using the same handler. As a demonstration, here are two equivalent examples of the new syntax:
except ValueError as e: except (ValueError, TypeError) as e:
Python 2.6 accepts both forms when creating exception handlers, helping with the porting process. More information about this change can be found in PEP 3110.
The most popular syntax for raising exceptions in 2.x looks like this:
raise ValueError, e
To truly emphasize that you're creating an instance of an exception, the only syntax supported in 3.x is this: