db_name and backend were initialized inside the try block but referenced in the except handler. If an exception occurred before their assignment (e.g. in resolve_database_id), the except block would raise: cannot access local variable 'db_name' where it is not associated Moved initialization before try with safe defaults.