Python: Code Challenges (Optional)
Optional code challenge to test your Python knowledge!
StartKey Concepts
Review core concepts you need to learn to master this subject
Handling Exceptions in Python
or
Operator
Comparison Operators
if
Statement
else
Statement
and
Operator
elif
Statement
Equal Operator ==
Handling Exceptions in Python
Handling Exceptions in Python
def check_leap_year(year):
is_leap_year = False
if year % 4 == 0:
is_leap_year = True
try:
check_leap_year(2018)
print(is_leap_year)
# The variable is_leap_year is declared inside the function
except:
print('Your code raised an error!')
A try
and except
block can be used to handle error in code block. Code which may raise an error can be written in the try
block. During execution, if that code block raises an error, the rest of the try
block will cease executing and the except
code block will execute.