Cannot catch Deadline API exception

Hi,

I have been working on the project where I am using Deadline api and have run in to the issue where I cannot catch api exception using try-except block. I hope you can help me with the issue.

I am trying to create an aid in case Deadline cannot be reached. I want to give it 3 shots to try to connect with 10 seconds gaps. Then If it cannot be accessed I raise an exception. I am doing this where the api is used for the first time.

Python version used: 2.7.13

Here is the piece of code that works:

import os
import sys
import time


class DeadlineAPIServerUnavailable(Exception):
    pass

if os.path.exists(r'\\deadline\DeadlineRepository\api\python'):
    sys.path.append(r'\\deadline\DeadlineRepository\api\python')

# try to import python lib from deadline repo
deadline_api = False
deadline_obj = None
try:
    import Deadline.DeadlineConnect as Connect
    deadline_obj = Connect.DeadlineCon('deadline', 8082)
    deadline_api = True
except ImportError as e:
    deadline_api = False


class Deadline(object):
    def __init__(self):
        self.api = deadline_api
        self.deadline_obj = deadline_obj

        deadline_path = os.getenv("DEADLINE_PATH") or ''

        self.command = os.path.join(deadline_path, 'deadlinecommand.exe')

        if self.api:

            for i in range(0, 4):

                if i > 2:
                    raise DeadlineAPIServerUnavailable('Deadline service unavailable')

                try:
                    # here I cannot catch exception
                    self.deadline_version = self.deadline_obj.Repository.GetDeadlineMajorVersion()
                    print(self.deadline_version)
                    
                    # test example that works
                    # print(10/0)

                    print('no exception')
                    break
                except:
                    print('exception')
                    time.sleep(1)


if __name__ == '__main__':
    dl = Deadline()

To cause an error(simulate network drop or Deadline is unreachable) I change this line

deadline_obj = Connect.DeadlineCon(‘deadline’, 8082)

to

deadline_obj = Connect.DeadlineCon(‘deadline’, 18082)

and I get expected error message

Traceback (most recent call last):
  File "\\deadline\DeadlineRepository\api\python\Deadline\DeadlineSend.py", line 52, in send
    response = urlopen(request, context=context)
  File "C:\Python27\lib\urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 429, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 447, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1198, in do_open
    raise URLError(err)
URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>
None
no exception

The question:

Even though I am using try - except block, that didn’t catch this error like all other exceptions. Why that happens? Why I was not able to catch it?

I have marked the place of the issue with a comment # here I cannot catch exception.

Just for test, line can be find it in the block, I have put print(10/0) and that is handled properly.
Error caused by line self.deadline_obj.Repository.GetDeadlineMajorVersion() was not?

Hope you will be able to help and thank you in advance for looking into this issue.