My question is about good practices of DB exception handling.
Let say I've got an application that stores some data in DB. There are the following layers implemented:
The question is: shall I handle an exception of database access, for example when adding a new record (assuming that the record should always be added correctly) ? Shall I just try-catch the exception in DatabaseAdapter and add it to log? Or maybe I shouldn't catch it at all?
Most of the time, an Exception when querying a database is the result of a "development time" mistake, like a malformed query, or modifying your schema but not incrementing the database version, or something like that. These will be pretty easy to find and fix right on the spot, so your end users won't be effected by these types of mistakes.
However, there are also real possibilities that you may have exceptions in the following cases:
onUpgrade()
method or something like that.Really, there's no universal answer for these scenarios. For the end user, showing a simple error is much nicer than having your app force close for example.
My rule of thumb is to avoid corruption of your database at all costs. I would much rather throw a RuntimeException
and immediately kill the app rather than having it silently do something wrong. Also, I'd rather show a message saying that an insert could not be completed rather than force closing the app.
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