<p>我有一个脚本,它使用<code>pygetwindow</code>模块在特定窗口上执行一些操作。脚本运行时,出现以下异常:</p>
<pre><code>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.
</code></pre>
<p>我对异常的发生没有意见,但是我想明确地捕捉这个异常。我已执行以下操作以尝试捕获此异常:</p>
<pre><code>try:
#implementation
except pygetwindow.PyGetWindowException:
#handle exception
</code></pre>
<p>及</p>
<pre><code>try:
#implementation
except PyGetWindowException:
#handle exception
</code></pre>
<p>以上两种方法都不能捕捉到例外。如果我使用上述任一项,我会得到另一个例外:</p>
<pre><code>NameError: name 'PyGetWindowException' is not defined
</code></pre>
<p>或</p>
<pre><code>NameError: name 'pygetwindow' is not defined
</code></pre>
<p>我不想捕获一般的<code>Exception</code>然后处理它,因为在其他异常的情况下,我希望以不同的方式处理它。我试图捕获此异常的方式是否有问题,或者是否有方法完全避免此异常</p>
<p>编辑:非常清楚,我已经导入了<code>pygetwindow</code></p>