B
B
beduin012018-03-19 17:17:42
Python
beduin01, 2018-03-19 17:17:42

What exceptions should be caught in SQLAlchemy?

def setup_connection():
  engine = create_engine("postgresql://[email protected]:5432/testdb", echo=False)
  Session = sessionmaker(bind=engine)
  sess = Session()
  try:
    Base.metadata.create_all(engine)
  except Exception as e:
    print("Could not initialize DB: {0}".format(e))
  return sess

session = setup_connection()

What kind of exceptions does it make sense to catch in SQLAlchemy? How am I doing it right here? Is it worth catching here not the base Exception, but `SQLAlchemyError` ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pasechnik Kuzmich, 2018-03-19
@Hivemaster

Never catch Exception. This leads to the appearance of hidden errors and unstable states, and also leads to heavy debugging. As a last resort, it is worth catching SQLAlchemyError. But in general, the more specific the error handler, the better.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question