0

I'm using Python WMI module to collect hardware information from a Windows system. This line raises an exception:

numa = c.Win32_PerfFormattedData_PerfOS_NUMANodeMemory()
Traceback (most recent call last):
  File "wmi.py", line 1209, in __getattr__
  File "wmi.py", line 1220, in _cached_classes
  File "<COMObject winmgmts:>", line 3, in Get
  File "win32com\client\dynamic.py", line 287, in _ApplyTypes_
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'SWbemServicesEx', None, None, 0, -1073738817), None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "health_check.py", line 1138, in <module>
  File "health_check.py", line 1121, in main
  File "health_check.py", line 228, in hardware_info
  File "wmi.py", line 1211, in __getattr__
  File "win32com\client\dynamic.py", line 527, in __getattr__
AttributeError: winmgmts:.Win32_PerfFormattedData_PerfOS_NUMANodeMemory

So I tried to catch it like this:

import pywintypes.com_error
try:
    numa = c.Win32_PerfFormattedData_PerfOS_NUMANodeMemory()

except pywintypes.com_error:
    logger.error('Failed to read NUMA status.')

But the import fails:

Traceback (most recent call last):
  File "health_check.py", line 20, in <module>
ModuleNotFoundError: No module named 'pywintypes.com_error'; 'pywintypes' is not a package
[9464] Failed to execute script health_check

I tried not importing the com_error class but that didn't work either. How can I find out the correct import to catch this type of exception? After importing wmi the module exists here:

>>> sys.modules['pywintypes']
<module 'pywintypes' (C:\WINDOWS\SYSTEM32\pywintypes37.dll)>
>>> sys.modules['pywintypes'].com_error
<class 'pywintypes.com_error'>
Elliott B
  • 980
  • 9
  • 32
  • That has nothing to do with using the correct or incorrect exception class. Import the module: `import pywintypes`. You can't specify a non-module in `import thing.whatever`. – user2357112 Sep 19 '20 at 01:55
  • Does this answer your question? [How to catch these exceptions individually?](https://stackoverflow.com/questions/25053785/how-to-catch-these-exceptions-individually) – fooiey Sep 19 '20 at 01:56
  • I tried that; it doesn't catch the exception – Elliott B Sep 19 '20 at 01:59
  • @fooiey very helpfu, thanks! `BaseException` works. But the heart of my question is how can you figure this out, given a new (and possibly undocumented) module. Is there some kind of introspection that will tell you the right exception class? – Elliott B Sep 19 '20 at 02:05
  • Maybe I've just answered my own question. After catching `BaseException as e` it tells me it's an AttributeError so then I could use that. – Elliott B Sep 19 '20 at 02:07

1 Answers1

0

WMI package has the creation of it’s own exception type. It looks like it raises it in your example.

from wmi.wmi import x_wmi

try:
    numa = c.Win32_PerfFormattedData_PerfOS_NUMANodeMemory()

except x_wmi:
    logger.error('Failed to read NUMA status.')

Andrey Borzenko
  • 718
  • 5
  • 8