Monday, December 30, 2013

How to deal with "QuickBooks has encountered a problem and needs to close."

It's 2 days before the end of the year, a time for bookkeeping and regulatory filing, albeit not a lot of time left to meet the end-of-year deadlines, when this happens when launching QuickBooks:

Trying again produces the same error, over and over again. Then panic sets in, did I just lost all my financial information ? Why is this happening ? Probably my company file got corrupted, and QuickBooks can't deal with it. More on this later. Let's fix it first.

One way to recover from a corrupted company file is to restore it from a previously saved backup. If you don't have a recent backup ready, my method won't help you. First I need to locate the backup file. QuickBooks has the habit of moving the location of backups, as you upgrade to newer versions, so my best bet is to do a search for files with the extension ".QBB":


Alternatively, you may have a copy of the backup saved "in the cloud" - highly recommended in case something happens to your computer - not necessarily to use the online storage offered by Intuit. Any alternative will do.

Trying to open a local backup by double-clicking the file produces this message:


Yeah, thats very helpful, Intuit. I'm having problems exactly with opening QuickBooks, which by default will always try to open the company file. If that's corrupted, I'll keep getting the "encountered a problem" error message (meaning that in fact the program has crashed). To get out of that dead-end we could first locate the company file (should have the extension *.QBW - if you don't know it's location you can search, as I did above for *QBB).


 Then we can rename it to something else:


Now if I try to open QuickBooks I see:


Now we're getting somewhere. Click the "Open or restore an existing company" button:


And choose the "Restore a backup copy":


In my case I'll use a local backup:


Here QuickBooks will try to hold my hand:


But the original (corrupted) company file would have been already renamed, so I'll just use its original name:


Next, sweet victory:


We're back in business. Just remember, because this was recovered from a backup, some of the most recent transactions, since that backup was made, will be missing. You'll need to enter those again. That's why it's good practice to perform backups often.

Now let's talk about why this happens, and how Intuit could have dealt with it in a better way and not have us jump through all those hoops. I'm a software developer. It's right up my ally.

In my case the problem happened because I inadvertently launched QuickBooks twice. My computer had just rebooted after Microsoft installed a bunch of updates, and now was acting pretty sluggish (thanks Microsoft !). My initial attempt to open QuickBooks seemed to go nowhere, so I tried to open it up again. Upon closer inspection, using the task manager, I was able to see 2 instances of the QuickBooks app running. Uh-oh. Shortly after came the error message about the crash.

So the first failure of Intuit was to handle this common scenario, of trying to open the program multiple times. Obviously, when QuickBooks opens it must be writing something in the company file, and having multiple instances open causes them to step on each other's toes and results in corrupting that file. There are plenty of ways to ensure exclusive access to a file by a single process - pretty basic stuff - which Intuit failed to implement.

There are other ways in which the company file may get corrupted, for example if something happens with the application/operating system/computer while the file is in the process of being written. There are ways of dealing with those scenarios too, and indeed a well engineered app should try to prevent errors and data corruption. But those may happen anyway, despite our best intentions. So the next step would be automatic error recovery. Here Intuit fails again. They pretty much try to open the same corrupted file each time the app launches, resulting in crashing over and over.

A better way would be to catch that error and not let the app crash, or at least on subsequent launches detect that the app had previously crashed, and enter a recovery mode. The recovery could be made from a redundant copy of the company file or from a backup, as I manually did above.

These are 2 principles worth observing when doing software development. First do your best to prevent errors. Then assume that some errors will happen anyway and ensure that you can recover from them graciously.