How can I raise an exception in Python so that it can later be caught via an except
block?
Just to note: there are times when you DO want to handle generic exceptions. If you're processing a bunch of files and logging your errors, you might want to catch any error that occurs for a file, log it, and continue processing the rest of the files. In that case, a
try:
foo()
except Exception as e:
print(str(e)) # Print out handled error
block is a good way to do it. You'll still want to raise
specific exceptions so you know what they mean, though.