Let's assume that we have the following flow:
def flow(input_val: Any) -> Any: result1 = function1(input_val) result2 = function2(result1) result3 = function3(result2) return result3 And let's say that I want to be able to catch exceptions for each of these three steps:
def flow(input_val: Any) -> Any: try: result1 = function1(input_val) except Exception as ex: print("Function 1 error: "+str(ex)) try: result2 = function2(result1) except Exception as ex: print("Function 2 error: "+str(ex)) try: result3 = function3(result2) except Exception as ex: print("Function 3 error: "+str(ex)) return result3 This doesn't look like the best way of handling exceptions in a flow like this, because if the first exception is caught, then result1 won't be defined. Also, if the third exception is caught, there won't be anything to return.
What's the best way of handling these situations?
result1so your code can continue, return early, or raise an exception (the same one you just caught or a new one) to prevent the rest offlowfrom trying to execute withoutresult1.function1raises, do you really want to attemptfunction2?