Search code examples
pythonpostgresqlpython-3.xplpy

PostgreSQL with Python catching exceptions with plpy.SPIError


how to properly catch the exception with plpy.SPIError,
notation:

try:
    # code
except plpy.SPIError,e:
    plpy.notice(e)

results in an error

DETAIL: SyntaxError: invalid syntax (<string>, line 99)

PostgreSQL 9.2 ,

PL / Python 3.2


Solution

  • In 3.x you've got to separate the exception from the variable with as:

    try:
        pass
    except plpy.SPIError as e:
        plpy.notice(e)
    

    The reason for the change from the syntax supported in 2.x ("syntactic ambiguity") is discussed here: http://legacy.python.org/dev/peps/pep-3110/