I have a program which has a lot of functions inside. And all this functions calls another functions. This functions has try catch blocks for to handle errors. I have to handle this errors without raising errors, because i am trying to do api application, i am loosing the control of handling errors.
So i wrote following code block, how can i solve this problem in a pythonic way?
def second_funct(param1):
_return = True
_error_message = ""
try:
print("second_funct")
#some processing
print(1/0)
except Exception as e:
_return = False
_error_message = e
return _return, _error_message
def third_funct(param2):
_return = True
_error_message = ""
try:
print("third_funct")
#some processing
except Exception as e:
_return = False
_error_message = e
return _return, _error_message
def main():
_return = True
_error_message = ""
_param1 = 0
_param2 = 0
try:
print("main_funct")
#some processing
_return, _error_message = second_funct(_param1)
if(_return):
print("go third_funct")
_return, _error_message = third_funct(_param2)
if(_return):
#some process
print("ok")
else:
print(_error_message)
else:
print(_error_message)
except Exception as e:
print(e)
main()
Result of this code ;
main_funct
second_funct
division by zero
Here a example what you can do, when using a decorator, this would use an Error handling like in golang, because u should always handle Errors.
Update, a Version using a diffrent approche without an decorator.