---
title: "How to check that a Python function throws an exception?"  
description: "How to check that a Python function throws an exception?"  
author: "Revati S Misra"  
published: 2023-07-20  
updated: 2023-07-20  
canonical: https://www.mindstick.com/forum/159169/how-to-check-that-a-python-function-throws-an-exception  
category: "python"  
tags: ["exception handling", "exception", "python"]  
reading_time: 2 minutes  

---

# How to check that a Python function throws an exception?

How to [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) that a [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) throws an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling)?

## Replies

### Reply by Aryan Kumar

Sure, there are a few ways to check that a Python function throws an exception.

One way is to use the `assertRaises()` function from the `unittest` module. The `assertRaises()` function takes two arguments: the first argument is the exception that you want to check for, and the second argument is the function that you want to call.

For example, the following code checks that the `divide_numbers()` function throws a `ZeroDivisionError` exception if the denominator is zero:

Python

```plaintext
import unittest

def divide_numbers(x, y):
    return x / y

class TestDivideNumbers(unittest.TestCase):
    def test_divide_by_zero(self):
        with self.assertRaises(ZeroDivisionError):
            divide_numbers(10, 0)

if __name__ == "__main__":
    unittest.main()
```

Another way to check that a Python function throws an exception is to use the `assert()` statement. The `assert()` statement takes two arguments: the first argument is the expression that you want to check, and the second argument is the error message that you want to print if the expression is false.

For example, the following code checks that the `divide_numbers()` function throws a `ZeroDivisionError` exception if the denominator is zero:

Python

```plaintext
def divide_numbers(x, y):
    return x / y

def test_divide_by_zero():
    assert divide_numbers(10, 0) == "Error: Can't divide by zero", "The function did not throw an exception."

if __name__ == "__main__":
    test_divide_by_zero()
```

Finally, you can also use the `try`/`except` block to check that a Python function throws an exception. The `try`/`except` block is a control flow statement that allows you to execute a block of code and handle any exceptions that are raised.

The following code checks that the `divide_numbers()` function throws a `ZeroDivisionError` exception if the denominator is zero:

Python

```plaintext
def divide_numbers(x, y):
    return x / y

def test_divide_by_zero():
    try:
        divide_numbers(10, 0)
    except ZeroDivisionError:
        pass
    else:
        raise Exception("The function did not throw an exception.")

if __name__ == "__main__":
    test_divide_by_zero()
```


---

Original Source: https://www.mindstick.com/forum/159169/how-to-check-that-a-python-function-throws-an-exception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
