How to catch specific exceptions on sqlalchemy?
问题 I want to catch specific exceptions like UniqueViolation on sqlalchemy. But sqlalchemy throw exceptions only through IntegrityError . So I catched specific exceptions with below code. except sqlalchemy.exc.IntegrityError as e: from psycopg2 import errors if isinstance(e.orig, errors.UniqueViolation): pass elif isinstance(e.orig, errors.ForeignKeyViolation): pass But it looks doesn't elegant. I don't want to using if statement just catch with specific exception name. Is there any way to solve