The raise statement can be used to throw an exception. The syntax of raise statement is:
raise exception-name [(optional argument)]
The argument is generally a string that is displayed when the exception is raised. For example, when an exception is raised as shown in Figure. the message “OOPS : An Exception has occurred” is displayed along with a brief description of the error.
Use of the raise statement to throw an exception

The error detected may be a built-in exception or may be a user-defined one. Consider the example given in Figure. that uses the raise statement to raise a built-in exception called IndexError.
In Figure. since the value of variable length is greater than the length of the list numbers, an IndexError exception will be raised. The statement following the raise statement will not be executed. So the message “NO EXECUTION” will not be displayed in this case.
As we can see in Figure. in addition to the error message displayed, Python also displays a stack Traceback. This is a structured block of text that contains information about the sequence of function calls that have been made in the branch of execution of code in which the exception was raised. In Figure. the error has been encountered in the most recently called function that has been executed.
Use of raise statement with built-in exception
