<aside> 👨🏼‍💻 In this page you will find some definitions and examples related to Errors & Exceptions in Python

</aside>

Support my work buying me a coffee ☕

Martí Grau is R&D and IA engineer


raise

Use raise to throw an exception if condition occurs

x = 10
if x > 5:
	raise Exception('...')

Assert

Use assert for certain condition

# This example will check if x is higher than 20. 
# It will throw an exception if not.
x = 10
assert x >= 20, "x is higher than 20!"

Try & Except

Use try & except to catch and handle exceptions

try:
	x >= 20
except AssertionError as error:
	print(error)