How to use "raise" keyword in Python

PythonKeywordRaise

Python Problem Overview


I have read the official definition of "raise", but I still don't quite understand what it does.

In simplest terms, what is "raise"?

Example usage would help.

Python Solutions


Solution 1 - Python

It has 2 purposes.

https://stackoverflow.com/a/13957849/20862">jackcogdill has given the first one.

> It's used for raising your own errors.

> if something: raise Exception('My error!')

The second is to reraise the current exception in an exception handler, so that it can be handled further up the call stack.

try:
  generate_exception()
except SomeException as e:
  if not can_handle(e):
    raise
  handle_exception(e)

Solution 2 - Python

raise without any arguments is a special use of python syntax. It means get the exception and re-raise it. If this usage it could have been called reraise.

    raise

From The Python Language Reference: > If no expressions are present, raise re-raises the last exception that > was active in the current scope.

If raise is used alone without any argument is strictly used for reraise-ing. If done in the situation that is not at a reraise of another exception, the following error is shown: RuntimeError: No active exception to reraise

Solution 3 - Python

It's used for raising errors.

if something:
    raise Exception('My error!')

Some examples here

Solution 4 - Python

Besides raise Exception("message") and raise Python 3 introduced a new form, raise Exception("message") from e. It's called exception chaining, it allows you to preserve the original exception (the root cause) with its traceback.

It's very similar to inner exceptions from C#.

More info: https://www.python.org/dev/peps/pep-3134/

Solution 5 - Python

You can use it to raise errors as part of error-checking:

if (a < b):
    raise ValueError()

Or handle some errors, and then pass them on as part of error-handling:

try:
    f = open('file.txt', 'r')
except IOError:
    # do some processing here
    # and then pass the error on
    raise

Solution 6 - Python

raise causes an exception to be raised. Some other languages use the verb 'throw' instead.

It's intended to signal an error situation; it flags that the situation is exceptional to the normal flow.

Raised exceptions can be caught again by code 'upstream' (a surrounding block, or a function earlier on the stack) to handle it, using a try, except combination.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCapurnicusView Question on Stackoverflow
Solution 1 - PythonIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 2 - PythonSohail SiView Answer on Stackoverflow
Solution 3 - PythonjackcogdillView Answer on Stackoverflow
Solution 4 - PythonStan ProkopView Answer on Stackoverflow
Solution 5 - Pythonsampson-chenView Answer on Stackoverflow
Solution 6 - PythonMartijn PietersView Answer on Stackoverflow