© Joannah Nanjekye 2017
Joannah NanjekyePython 2 and 3 Compatibilityhttps://doi.org/10.1007/978-1-4842-2955-2_6

6. Exceptions

Joannah Nanjekye
Kampala, Uganda
Python provides a very concise way to handle exceptions, but Python 2 and 3 each have their own distinct syntax for exception handling. In Python 3, catching exception objects requires the as keyword; raising exceptions with arguments requires parentheses; and strings cannot be used as exceptions. This chapter describes how to achieve neutral compatibility for raising, catching exceptions, and exception chaining.

Raising Exceptions

Python allows us to raise exceptions in several ways using the raise statement. Other languages may use throw. We can specify three parameters when using the ...

Get Python 2 and 3 Compatibility: With Six and Python-Future Libraries now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.