xxxxxxxxxx
#assert <condition> , <message when condition is False (before halting the code)>
#assertion error happens when <condition> is False on e.g.
divisor = 0
assert divisor!=0 , "exiting due to zero divisor"
# if we run that on interactive Python interpreter:
>>> assert divisor!=0 , "exiting due to zero divisor"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: exiting due to zero divisor
assert error lets us terminate code execution and tells us why
xxxxxxxxxx
try:
# Your code containing the assertion statement
assert condition, "Assertion Error Message"
except AssertionError as e:
# Handle the assertion error
print("Caught AssertionError:", str(e))
# Optionally, provide additional error handling or logging