title: Python / win32com / try and except / check if application is running
body: <p>I'm using python 3.6.2 on Windows 7.</p>
  
  <p>I have a little function, that should check if MS Excel is already running or not. The function is behaving a little bit strange, here your help would be very appreciated.</p>
  
  <p>The function should check, if Excel is running. If yes, print text and exit the application. If no, everything is fine, continue.</p>
  
  <p>My Problem now is, when Excel <strong>is</strong> running the try-block is executed including the printing but not the sys.exit() <strong>AND</strong> the except-block is executed as well!?
  If Excel is <strong>NOT</strong> running, then everything is fine, the try-block aborts and only the except-block is executed.</p>
  
  <p>Why is it executing both print-statements, when Excel is running?</p>
  
  <p>Please help!</p>
  
  <p>Here's my code:</p>
  
  <pre><code>def check_if_Excel_runs():
      import win32com.client
      import sys
      try:
          win32com.client.GetActiveObject("Excel.Application")
          # If there is NO error at this stage, Excel is already running
          print('Excel is running, please close first')
          sys.exit()
      except:
          print('Excel is NOT running, this is good!')
      return
  
  check_if_Excel_runs()
  </code></pre>
  
  <p>My output (when Excel is running):</p>
  
  <pre><code>Excel is running, please close first
  Excel is NOT running, this is good!
  </code></pre>
  
  <p>Thanks in advance!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Ok, I have understood, that I should not generally do "except" without specifiying the exception I want to handle. But how do I determin, of which type the exception is, that I want to catch. If I look at the error message, it is not clear to me.</p>
  
  <pre><code>com_error                                 Traceback (most recent call last)
  &lt;ipython-input-39-70980aa1c5df&gt; in &lt;module&gt;()
       11     return
       12 
  ---&gt; 13 check_if_Excel_runs()
  
  &lt;ipython-input-39-70980aa1c5df&gt; in check_if_Excel_runs()
        3     import sys
        4     try:
  ----&gt; 5         win32com.client.GetActiveObject("Excel.Application")
        6         # If there is NO error at this stage, Excel is already running
        7         print('Excel is running, please close first')
  
  c:\users\chnn\appdata\local\programs\python\python36-32\lib\site-packages\win32com\client\__init__.py in GetActiveObject(Class, clsctx)
       77   """  
       78   resultCLSID = pywintypes.IID(Class)
  ---&gt; 79   dispatch = pythoncom.GetActiveObject(resultCLSID)
       80   dispatch = dispatch.QueryInterface(pythoncom.IID_IDispatch)
       81   return __WrapDispatch(dispatch, Class, resultCLSID = resultCLSID, clsctx = clsctx)
  
  com_error: (-2147221021, 'Operation unavailable', None, None)
  </code></pre>
  
  <p>Thanks again for your help, folks!</p>