<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
Use raise to throw an exception if condition occurs
x = 10
if x > 5:
raise Exception('...')
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!"
Use try & except to catch and handle exceptions
try:
x >= 20
except AssertionError as error:
print(error)
Additionally it can use else & finally conditions
else:
#To instruct a program to excecute certain block of code
#only in the absence of exceptions.
finally:
#Implement some sort of code/action after executing your code
#Lets you execute code, regardless of the result of the try- and except blocks