Key Concepts

Review core concepts you need to learn to master this subject

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.