Here is the simple code:
import sys
class EmptyArgs(StandardError):
pass
if __name__ == "__main__":
# The first way to raise an exception
if len(sys.argv) == 1:
raise EmptyArgs
# The second way to raise an exception
if len(sys.argv) == 1:
raise EmptyArgs()
Which way is "more" correct? Both are working.
Note: In my real code, the exception is exactly the same as I declared: without a message and arguments.
Both are proper; the latter form lets you attach arguments to your exception:
See the documentation for the
raisestatementPython 2 had a few more options, these have been dropped from Python 3, where you could pass the exception class and arguments as a tuple, but Python 2 is now long gone.