What is the upper level exception that I can catch SQLAlechmy exceptions with ?
>>> from sqlalchemy import exc >>> dir(exc) ['ArgumentError', 'CircularDependencyError', 'CompileError', 'ConcurrentModificationError', 'DBAPIError', 'DataError', 'DatabaseError', 'DisconnectionError', 'FlushError', 'IdentifierError', 'IntegrityError', 'InterfaceError', 'InternalError', 'InvalidRequestError', 'NoReferenceError', 'NoReferencedColumnError', 'NoReferencedTableError', 'NoSuchColumnError', 'NoSuchTableError', 'NotSupportedError', 'OperationalError', 'ProgrammingError', 'SADeprecationWarning', 'SAPendingDeprecationWarning', 'SAWarning', 'SQLAlchemyError', 'SQLError', 'TimeoutError', 'UnboundExecutionError', 'UnmappedColumnError', '__builtins__', '__doc__', '__file__', '__name__', '__package__'] >>>
exception sqlalchemy.exc. ArgumentError(*arg, **kw) Raised when an invalid or conflicting function argument is supplied. This error generally corresponds to construction time state errors.
The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.
The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error.
To catch any exception SQLAlchemy throws:
from sqlalchemy import exc db.add(user) try: db.commit() except exc.SQLAlchemyError: pass # do something intelligent here
See help(sqlalchemy.exc) and help(sqlalchemy.orm.exc) for a list of possible exceptions that sqlalchemy can raise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With