How can I raise an exception in Python so that it can later be caught via an except
block?
Read the existing answers first, this is just an addendum.
Notice that you can raise exceptions with or without arguments.
Example:
raise SystemExit
exits the program but you might want to know what happened.So you can use this.
raise SystemExit("program exited")
this will print "program exited" to stderr before closing the program.