Syntax
try:
# code that can potentially break
except:
# code to handle the errors
finally:
# something that will be done
# irrespective of occurance of error(s)
In [6]: 1 correct_input = False
2
3 while not correct_input:
4 user_age = input("Enter your age:")
5 user_salary = input("Enter your salary:")
6 try:
7 user_age = int(user_age)
8 user_salary = int(user_salary)
9 except:
10 print("You have not entered a numeric value.")
11 user_age = 0
12 user_salary = 0
13 finally:
14 print("Hello from 'finally'!")
15
16 if user_age >= 0 and user_age < 200:
17 correct_input = True
18 else:
19 print("Invalid age. Try again!")
20
21 if user_age < 18:
22 print("Not an adult! No beers for you!")
23 else:
24 print("Welcome")
Enter your age:32
Enter your salary:34
Hello from 'finally'!
Welcome