Mytzenka
Mytzenka

Reputation: 245

How to catch a com_error

My python program uses pythoncom (pythoncomxx.dll). I am sometimes getting a 'com_error' exception (which is fine) and I would like to catch it in an exception handler.

But when I try to do the following, the compiler complains that 'com_error' is unknown.

try:
  self.session.findById(tree_id).expandNode(node_id)
except com_error:
  print_exc()

I added the following to my imports, but this doesn't help: the statement 'from pythoncom import com_error' shows 'com_error' underlined in red

import pythoncom
import win32com.client
import time
from traceback import print_exc
from pythoncom import com_error

The 'solution' is to catch just all exceptions, but i want to catch only this one. How can I make the 'com_error' known to the program?

Thanks!

Upvotes: 2

Views: 3588

Answers (2)

Peter
Peter

Reputation: 371

Importing pythoncom helped me. Same as you I use win32com.client, where the error has it's origin.

import pythoncom
    ...
try:
    ...
except pythoncom.com_error:
    ...

Reference

Upvotes: 1

Dustin Spicuzza
Dustin Spicuzza

Reputation: 836

I don't know anything specifically about pythoncom, but it seems from google searching that the import is correct. Have you tried running the code even with the red underline present?

Because python is a dynamic language, not all errors underlined in red are actual errors. Some IDE's cannot inspect the contents of compiled extensions such as pythoncom.

Upvotes: 2

Related Questions