I am writing a Python program that interfaces with Quickbooks. When connecting to Quickbooks, depending on the problem, I might get one of two common exceptions:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'QBXMLRP2.RequestProcessor.2', 'The QuickBooks company data file is currently open in a mode other than the one specified by your application.', None, 0, -2147220464), None)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'QBXMLRP2.RequestProcessor.2', 'Could not start QuickBooks.', None, 0, -2147220472), None)
Catching the generic exception with except Exception as e
shows that the type of e
is <class 'pywintypes.com_error'>
, which cannot be used to catch an exception:
... catch pywintypes.com_error as e:
NameError: global name 'pywintypes' is not defined
So how might I catch these two exceptions in a non-generic manner? Ideally the code would have this layout:
try:
qb = qbsdk_interface.Qbsdk_Interface(QB_FILE)
except QbWrongModeError as e:
print('Quickbooks is open in the wrong mode!')
except QbClosedError as e:
print('Quickbooks is closed!')
except Exception as e:
print('Something else went wrong!')
Of course, the exceptions QbWrongModeError
and QbClosedError
do not exist, so what should be there in their place?