An AssertionError
is raised when the assert statement fails:
x = "hello"
#if condition returns True, then nothing happens:
assert x == "hello"
#if condition returns False, AssertionError is raised:
assert x == "goodbye"
Try it Yourself » Definition and UsageThe AssertionError
exception occurs when an assert statement fails.
You can handle the AssertionError
in a try...except
statement, see the example below.
Read more about the assert keyword in our assert Keyword chapter.
Exception Handling ExampleHandling the AssertionError
in a try...except
statement:
x = "hello"
try:
assert x == "goodbye"
except AssertionError:
print("Error in assert statement")
except:
print("Something else went wrong")
Track your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4