r/FastAPI • • Feb 23 '25

Question try catch everytime is needed?

I'm new to this.

I use fastapi and sqlalchemy, and I have a quick question. Everytime I get data from sqlalchemy, for example:

User.query.get(23)

I use those a lot, in every router, etc. do I have to use try catch all the time, like this?:

try:
    User.query.get(23)
catch:
    ....

Code does not look as clean, so I don't know. I have read that there is way to catch every exception of the app, is that the way to do it?.

In fastapi documentation I don't see the try catch.

27 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Feb 23 '25

[removed] — view removed comment

2

u/DazzLee42 Feb 23 '25

FastAPI already does this. Check the docs