A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3schools.com/python/ref_exception_assertionerror.asp below:

Website Navigation


Python AssertionError Exception

Python AssertionError Exception

❮ Python Exceptions

Example

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 Usage

The 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 Example

Handling 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")

Try it Yourself »

❮ Python Exceptions

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