Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
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
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
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
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()
Join MindStick Community
You need to log in or register to vote on answers or questions.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, there are a few ways to check that a Python function throws an exception.
One way is to use the
assertRaises()function from theunittestmodule. TheassertRaises()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 aZeroDivisionErrorexception if the denominator is zero:Python
Another way to check that a Python function throws an exception is to use the
assert()statement. Theassert()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 aZeroDivisionErrorexception if the denominator is zero:Python
Finally, you can also use the
try/exceptblock to check that a Python function throws an exception. Thetry/exceptblock 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 aZeroDivisionErrorexception if the denominator is zero:Python