database: fix exception handling