Manually Raise Exceptions in Python

  1. Exceptions With the try...except Clause in Python
  2. Manually Raising Exceptions With the raise Statement in Python

In this tutorial, we will discuss methods to raise exceptions manually in Python.

Exceptions With the try...except Clause in Python

In Python, the program stops its execution if an error occurs. The try...catch clause provides a method to handle exceptions in Python automatically.

Using the try...catch clause, we can continue program execution even if some part of the code crashes. The suspicious code is placed in the try clause. The except clause is used to catch the exceptions raised in the try clause. If an exception occurs, the except clause is executed. Otherwise, the except clause is not executed, and the normal flow of the program continues. The following code example shows us how we can handle exceptions with the try...catch clause in Python.

try:
    x = 1/0
    
    print(x)
except Exception as e:
    print("Exception : " + repr(e))

Output:

Exception : ZeroDivisionError('division by zero')

In the above code, we first initialize a variable x, which is equal to 1/0. So, a ZeroDivisionError exception is automatically raised, and the except block is executed.

Manually Raising Exceptions With the raise Statement in Python

In the above section, we have discussed a method to handle exceptions with try...except clause automatically. In Python, we can also manually raise specific exceptions with the raise statement. We can use the raise statement inside an if statement to raise a specific exception if a specific condition occurs. The following code example shows us how we can manually raise specific exceptions with the raise statement in Python.

a =1
b =0

try:
    if b == 0:
        raise ZeroDivisionError("You cannot divide a number by zero")
        
        x = a/b
except Exception as e:
    print("Exception : " + repr(e))

Output:

Exception : ZeroDivisionError('You cannot divide a number by zero')

In the above code, we first initialize both a and b and then raise a ZeroDivisionError exception if the b is equal to 0. There are many types of exceptions that can be raised by the raise statement. Some of these exceptions are demonstrated in the below examples.

x = "A String variable"

try:
    if not type(x) is int:
        raise TypeError("x is not an integer")
except Exception as e:
    print("Exception : "+ repr(e))

Output:

Exception : TypeError('x is not an integer')

The above code raises a TypeError exception if the var is not an integer type variable. Another type of exception is shown in the code example below.

month = 13
try:
    if month > 12:
        raise ValueError("There cannot be more than 12 months in a year")
except Exception as e:
    print("Exception : "+ repr(e))

Output:

Exception : ValueError('There cannot be more than 12 months in a year')

The above code raises a ValueError exception if month is greater than 12.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python Exception

  • Multiple Exceptions in Python