How to handle errors with try and except by using else and finally in Python

  • If try throws an error then except executes.
  • If try doesn’t throw an error then else part executes.
  • The finally block executes always. Error doesn’t matter here.
while True:
    try:
        age = int(input("Enter the age to check eligibility : "))
        break
    except ValueError:
        print("Please enter an integer")
    except:
        print("Unexpected error")
    else:
        print(f"User has entered {age}")
    finally:
        print("This is a program to verify age")

if age>18:
    print("You are eligible to play")
else:
    print("You aren't eligible to play")

Leave a comment