Reading documentation on exception handling tends to focus on using spesific exception types – which is good advice in general.
However there are times I want to run some Python code which under no circumstances throws an exception that isn’t handled & stops the program from running:
For example, a graphical application might run a user defined script, I don’t know what errors that script could trigger, so it’s not useful to check for spesific errors:
try:
run_script_file(filepath)
except:
import traceback
print("Script:", filepath, "failed with error!")
traceback.print_exc()
This works, but code checking tools are warning that bare-except should not be used, while I could suppress the warnings in each case, I was considering using a except BaseException:
to quiet the warnings.
Is a bare except:
is guaranteed to be the equivalent of except BaseException:
or are there subtle differences where a bare except might catch exceptions a BaseException
wouldn’t?