Exceptions and error handling

One of the things users hate to see are bugs in programs, so there are many techniques for attempting to catch them before a user sees them. One that is routinely used is exception handling. However, some developers like to use exception handling instead of preventing the issue or gracefully handling it. For example, I was working on code to present errors (in a worst case scenario) to users and trapped the error handling. As I did this, I kept noticing strange exceptions being thrown when my app started. Turns out that Apple uses exceptions (which are benign) in Core Data for its parsing of XML. Personally I don’t think this is a great way of handling things, but who am I to judge?

On the same topic, I had a rash of users complain that they lost data in my apps. I was very perplexed as I’d never seen this. I took a look at the code where I save out the data and noticed that I had put the entire save routine along with some other routines in an exception block with the save being at the end. This meant that if there was a problem with the first chunk of code, the data would never get saved. While I still have no idea what would cause the first chunk of code to fail, I pulled the save out of the exception handling and have yet to receive a problem report (knock on wood).

So the moral of the story is to carefully watch how exceptions are used and don’t go overboard with exception handling.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.