If no expressions are present, raise re-raises the exception that is currently being handled, which is also known as the active exception. If there isn’t currently an active exception, a RuntimeError exception is raised indicating that this is an error.
Otherwise, raise evaluates the first expression as the exception object. It must be either a subclass or an instance of BaseException. If it is a class, the exception instance will be obtained when needed by instantiating the class with no arguments.
The type of the exception is the exception instance’s class, the value is the instance itself.
A traceback object is normally created automatically when an exception is raised and attached to it as the __traceback__ attribute. You can create an exception and set your own traceback in one step using the with_traceback() exception method (which returns the same exception instance, with its traceback set to its argument), like so:
The from clause is used for exception chaining: if given, the second expression must be another exception class or instance. If the second expression is an exception instance, it will be attached to the raised exception as the __cause__ attribute (which is writable). If the expression is an exception class, the class will be instantiated and the resulting exception instance will be attached to the raised exception as the __cause__ attribute. If the raised exception is not handled, both exceptions will be printed:
>>> try:... print(1 / 0)... except Exception as exc:... raise RuntimeError("Something bad happened") from exc...Traceback (most recent call last): File "<stdin>", line 2, in <module> print(1 / 0) ~~^~~ZeroDivisionError: division by zeroThe above exception was the direct cause of the following exception:Traceback (most recent call last): File "<stdin>", line 4, in <module> raise RuntimeError("Something bad happened") from excRuntimeError: Something bad happened
A similar mechanism works implicitly if a new exception is raised when an exception is already being handled. An exception may be handled when an except or finally clause, or a with statement, is used. The previous exception is then attached as the new exception’s __context__ attribute:
>>> try:... print(1 / 0)... except:... raise RuntimeError("Something bad happened")...Traceback (most recent call last): File "<stdin>", line 2, in <module> print(1 / 0) ~~^~~ZeroDivisionError: division by zeroDuring handling of the above exception, another exception occurred:Traceback (most recent call last): File "<stdin>", line 4, in <module> raise RuntimeError("Something bad happened")RuntimeError: Something bad happened
Exception chaining can be explicitly suppressed by specifying None in the from clause:
>>> try:... print(1 / 0)... except:... raise RuntimeError("Something bad happened") from None...Traceback (most recent call last): File "<stdin>", line 4, in <module>RuntimeError: Something bad happened
Additional information on exceptions can be found in section Exceptions, and information about handling exceptions is in section The try statement.
Changed in version 3.3: None is now permitted as Y in raise X from Y.
Added the __suppress_context__ attribute to suppress automatic display of the exception context.
Changed in version 3.11: If the traceback of the active exception is modified in an except clause, a subsequent raise statement re-raises the exception with the modified traceback. Previously, the exception was re-raised with the traceback it had when it was caught.