In this article, we will learn how the Python Raise keyword works with the help of examples and its advantages.
Python Raise Keyword
Python raise Keyword is used to raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program. It is used to bring up the current exception in an exception handler so that it can be handled further up the call stack.
Python Raise Syntax
raise {name_of_ the_ exception_class}
The basic way to raise an error is:
raise Exception("user text")
Checking whether an integer is odd or even
In the below code, we check if an integer is even or odd. if the integer is odd an exception is raised. a is a variable to which we assigned a number 5, as a is odd, then if loop checks if it's an odd integer, if it's an odd integer then an error is raised.
Python3 a = 5 if a % 2 != 0: raise Exception("The number shouldn't be an odd integer")
Output:

Checking Errror Type
We can check the type of error which have occurred during the execution of our code. The error can be a 'ValueError' or a 'ZeroDivisionError' or some other type of error.
Syntax: raise TypeError
Checking the error type
In the below code, we tried changing the string 'apple' assigned to s to integer and wrote a try-except clause to raise the ValueError. The raise error keyword raises a value error with the message "String can't be changed into an integer".
Python3 s = 'apple' try: num = int(s) except ValueError: raise ValueError("String can't be changed into integer")
Output

Raising an exception Without Specifying Exception Class
When we use the raise keyword, there's no compulsion to give an exception class along with it. When we do not give any exception class name with the raise keyword, it reraises the exception that last occurred.
Example
In the above code, we tried changing the string 'apple' to integer and wrote a try-except clause to raise the ValueError. The code is the same as before except that we don't provide an exception class, it reraises the exception that was last occurred.
Python3 s = 'apple' try: num = int(s) except: raise
Output:

Advantages of the raise keyword
- It helps us raise error exceptions when we may run into situations where execution can't proceed.
- It helps us raise error in Python that is caught.
- Raise allows us to throw one exception at any time.
- It is useful when we want to work with input validations.
Similar Reads
Python True Keyword True is a built-in Boolean value that represents truth or logical true value. It is one of the two Boolean constants (True and False) and is often used in conditions, loops and logical operations.Python treats True as 1 when used in arithmetic operations and as a truthy value in conditional statemen
2 min read
Python Keywords Keywords in Python are reserved words that have special meanings and serve specific purposes in the language syntax. Python keywords cannot be used as the names of variables, functions, and classes or any other identifier. Getting List of all Python keywordsWe can also get all the keyword names usin
2 min read
Keywords in Python | Set 2 Python Keywords - Introduction Keywords in Python | Set 1 More keywords:16. try : This keyword is used for exception handling, used to catch the errors in the code using the keyword except. Code in "try" block is checked, if there is any type of error, except block is executed. 17. except : As expl
4 min read
Python in Keyword The in keyword in Python is a powerful operator used for membership testing and iteration. It helps determine whether an element exists within a given sequence, such as a list, tuple, string, set or dictionary.Example:Pythons = "Geeks for geeks" if "for" in s: print("found") else: print("not found")
3 min read
as Keyword - Python as keyword in Python plays a important role in simplifying code, making it more readable and avoiding potential naming conflicts. It is mainly used to create aliases for modules, exceptions and file operations. This powerful feature reduces verbosity, helps in naming clarity and can be essential whe
3 min read