AssertionError
What is AssertionError?
AssertionError: A sanity-check assertion in the code evaluated to false, signalling that an assumption the programmer relied on turned out to be untrue. An AssertionError is raised when an assertion in the code turns out false. An assertion states something the developer believes must always be true, such as assert total >= 0. When it fails, that belief was wrong, and the program stops.
Frequently asked questions about AssertionError
How is AssertionError used in practice?
Assertions are a tool for catching bugs early. They document assumptions and fail loudly at the exact spot where reality differs from expectations, instead of letting bad data travel further.
What should you keep in mind with AssertionError?
They are not for validating user input. In Python and PHP, assertions can be switched off in production, so a check that protects against bad input would silently disappear. Use proper validation and exceptions for anything a user or another system can cause. Keep assertions for internal logic, and in tests, where assertion failures are exactly how you learn that something broke.
At a glance
| Category | Error types |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |