Skip to content

Latest commit

 

History

History
51 lines (35 loc) · 1.47 KB

File metadata and controls

51 lines (35 loc) · 1.47 KB

Use new raise syntax

Summary

The module uses the old raise syntax raise ErrorClass, args. Use the new raise syntax raise ErrorClass(args) to improve readability and ensure compatibility with Python versions 3 and above (which require the new syntax).

Description

One of Python's guiding maxims is "there should be one obvious way to do it." Providing multiple, functionally-equivalent ways to raise exceptions violates this maxim.

Examples

Module uses old raise syntax

The module below triggers the use new raise syntax error because of the statement raise Exception, "1 != 0". This syntax is deprecated in Python 3.

Warning

The code below is an example of an error. Using this code will create bugs in your programs!

try:
    if 1 != 0:
        raise Exception, "1 != 0"  # old raise syntax
except Exception as e:
    print e

Solutions

Use the new raise syntax

The updated module has replaced the old style raise statement raise Exception, "1 != 0" with the new style raise Exception("1 != 0").

try:
    if 1 != 0:
        raise Exception("1 != 0")  # new raise syntax
except Exception as e:
    print e

References