Python nested recursion for multi-conditional exception handling

Python nested recursion for multi-conditional exception handling … here is a solution to the problem.

Python nested recursion for multi-conditional exception handling

I have 3 conditions C1, C2, C3 to check. If C1 gives a timeout exception, try C2. If C2 gives a timeout exception, try C3. If C3 gives a timeout exception, None is returned for the function. However, if any of C1, C2, C3 succeed, the rest of the code in the function is executed.

I’m using a nested try catch block as shown below, but not sure if this is the right approach. Please suggest the best Python method.

def func():
  try:
    C1
  except:
    try:
      C2
    except:
      try:
        C3
      except:
         return None
  
try:
    ** Rest of the Code **
    return someValue
  except:
    return None

Solution

**You can follow this approach**
def func():
    if CheckC1() or CheckC2() or CheckC3() :
        try:
        ** Rest of the Code **
            return someValue
        except:
            return None
    return None

def CheckC1():
    try:
        C1
        return true
    except:
        return false

def CheckC2():
    try:
        C2
        return true
    except:
        return false

def CheckC3():
    try:
        C3
        return true
    except:
        return false

Related Problems and Solutions