Im trying to build a definition that does error checking for my definitions. What is the proper way to structure this code for error handling?
I want the script to run a specified definition, if it fails, retry a number of times, then kill it if it times out.
import time
def open_file():
open('testfile.py')
def run_def(definition):
for i in range(0, 4):
try:
definition
str_error = None
except Exception as str_error:
pass
if str_error:
time.sleep(1)
print(str_error)
else:
break
print('kill script')
run_def(open_file())
I get an error when I try passing the definition into the error check definition. But the script works if I dont put the error checker into a separate definition.
FileNotFoundError: [Errno 2] No such file or directory: 'testfile.py'
open_filefunction returnsNone. So doingrun_def(open_file())callsrun_defwith an arg ofNone. And sodefinitionin thetryblock won't ever raise an exception.run_def(open_file)then in function dodefinition(). Normally error will be catched.