Raising an Exceptions in Python

PythonServer Side ProgrammingProgramming

You can raise exceptions in several ways by using the raise statement. The general syntax for the raise statement is as follows.


raise [Exception [, args [, traceback]]]

Here, Exception is the type of exception (for example, NameError) and argument is a value for the exception argument. The argument is optional; if not supplied, the exception argument is None.

The final argument, traceback, is also optional (and rarely used in practice), and if present, is the traceback object used for the exception.


An exception can be a string, a class or an object. Most of the exceptions that the Python core raises are classes, with an argument that is an instance of the class. Defining new exceptions is quite easy and can be done as follows −

def functionName( level ):
   if level < 1:
      raise "Invalid level!", level
      # The code below to this would not be executed
      # if we raise the exception

Note − In order to catch an exception, an "except" clause must refer to the same exception thrown either class object or simple string. For example, to capture above exception, we must write the except clause as follows −

   Business Logic here...
except "Invalid level!":
   Exception handling here...
   Rest of the code here...
Updated on 30-Jan-2020 07:00:36