Handling errors gracefully
In even simple applications, it can be difficult to know or account for all the possible error conditions that could occur during the execution of a program. So, it is important to build in a way to catch all the errors that slip by normal handling and provide as graceful a handling of them as possible. This can be the difference between your users thinking your application is buggy or doesn't work and continuing to give it another chance. In this recipe, we will discuss how to install an exception hook to give feedback to users when an unexpected error occurs.
How to do it…
Here are the steps that you need to perform:
First, we need to start by importing a few extra modules to help out with this recipe, as follows:
import sys import traceback import wx import wx.lib.dialogs as DIA
Next, let's define an app object that can deal with unhandled exceptions through the following code:
class ErrorHandlingApp(wx.App): def OnInit(self): sys.excepthook = self.ExceptionHook...