Raise exception

You can manually throw (raise) an exception in Python with the keyword raise. This is usually done for the purpose of error-checking. Consider the following example:

try:
    raise ValueError
except ValueError:
    print('There was an exception.')

The code above demonstrates how to raise an exception. Here is the output:

>>> 
There was an exception.
>>>

You can use the raise keyword to signal that the situation is exceptional to the normal flow. For example:

x = 5
if x < 10:
    raise ValueError('x should not be less than 10!')

Notice how you can write the error message with more information inside the parentheses. The example above gives the following output (by default, the interpreter will print a traceback and the error message):

>>> 
Traceback (most recent call last):
  File "C:/Python34/Scripts/raise1.py", line 3, in <module>
    raise ValueError('x should not be less than 10!')
ValueError: x should not be less than 10!
>>>
Geek University 2022