When a Python program throws an Exception, the process returns a non-zero return code. Subprocess functions like call will return the return code by default. So, to check if an exception occurred, check for a non-zero exit code.
Here is an example of checking the return code:
retcode = subprocess.call("python test.py", shell=True)
if retcode == 0:
pass # No exception, all is good!
else:
print("An exception happened!")
Another method would be to use subprocess.check_call, which throws a subprocess.CalledProcessError exception on a non-zero exit status. An example:
try:
subprocess.check_call(["python test.py"], shell=True)
except subprocess.CalledProcessError as e:
print("An exception occured!!")
If you need to know which exception occurred in your test program, you can change the exception using exit(). For example, in your test.py:
try:
pass # all of your test.py code goes here
except ValueError as e:
exit(3)
except TypeError as e:
exit(4)
And in your parent program:
retcode = subprocess.call("python test.py", shell=True)
if retcode == 0:
pass # No exception, all is good!
elif retcode == 3:
pass # ValueError occurred
elif retcode == 4:
pass # TypeError occurred
else:
pass # some other exception occurred