In my python3 script, I am calling an external program with the subprocess module. When the called program exits with a non-zero status code, a CalledProcessError is thrown. In that case I print an error message and want to terminate the script in the exception handler.
My problem is, that exit() works by throwing a SystemExit exception itself, so I end up with:
During handling of the above exception, another exception occurred:
The script looks similar to this:
try:
output = subprocess.check_output(["program"])
return output
except subprocess.CalledProcessError as error:
print("program returned non-zero exit status", file=sys.stderr)
exit(error.returncode)
How can I terminate the script without throwing an exception?