0

I have a script which uses pygetwindow module to do some operations on a specific window. While the script runs, I get the following exception:

File "C:\Program Files (x86)\Python38-32\lib\site-packages\pygetwindow\_pygetwindow_win.py", line 237, in activate
    _raiseWithLastError()
  File "C:\Program Files (x86)\Python38-32\lib\site-packages\pygetwindow\_pygetwindow_win.py", line 97, in _raiseWithLastError
    raise PyGetWindowException('Error code from Windows: %s - %s' % (errorCode, _formatMessage(errorCode)))
pygetwindow.PyGetWindowException: Error code from Windows: 0 - The operation completed successfully.

I'm okay with the exception occurring but I want to catch this exception explicitly. I have done the following to try and catch this exception:

try:
  #implementation
except pygetwindow.PyGetWindowException:
  #handle exception

and

try:
  #implementation
except PyGetWindowException:
  #handle exception

Neither of the above catches the exception. If I use either of the above, I get another exception:

NameError: name 'PyGetWindowException' is not defined

or

NameError: name 'pygetwindow' is not defined

I don't want to catch the general Exception and then handle it since in case of other exceptions, I want to handle it differently. Is there something wrong in how I'm trying to catch this exception or is there a way to avoid this exception altogether?

EDIT: To be very clear, I have already imported pygetwindow.

mtm
  • 504
  • 1
  • 7
  • 25

2 Answers2

1

Update

From the source file, it was clear that in order to use PyGetWindowException, you need to import the exception specifically (and not just import pygetwindow). Therefore, in order to catch the exception, one will have to do:

from pygetwindow import PyGetWindowException

After this import, you can use the exception in the normal way:

try:
  #implementation
except PyGetWindowException:
  #handle exception

Update 2

Another general way to do this would be to get the exception name from general exception and compare.

try:
    try:
        #implementation
    except Exception as e:
        if e.__class__.__name__ == 'PyGetWindowException':
             #handle exception
        else:
             raise e

except Exception as e:
    #handle other exceptions except pygetwindow exception

Original answer (not recommended)

Found a way to solve this question in this answer.

From the source of pygetwindow, it was clear that, whenever PyGetWindowException is raised, it is accompanied by the text:

"Error code from Windows:"

which indicates the error code given by Windows.

Based on this information, I did the following:

try:
    try:
        #Implementation
    except Exception as e:
        if "Error code from Windows" in str(e)
            # Handle pygetwindow exception
        else:
            raise e
except Exception as e:
    #handle other exceptions

This is another way (although the first one and second one are the correct and straightforward solutions) to solve the problem.

mtm
  • 504
  • 1
  • 7
  • 25
0

You should have import pygetwindow at the begging of your script. It complains about not knowing what pygetwindow is.

jthulhu
  • 7,223
  • 2
  • 16
  • 33
  • 1
    Considering the exception given at the beginning of the post, if I hadn't imported `pygetwindow`, I would be getting a different exception altogether. I got the `NameError` after the other exception occurred, which wouldn't have occurred if I didn't import `pygetwindow` Therefore, it should be clear that I already have imported `pygetwindow`. – mtm Jul 31 '21 at 08:41