0% found this document useful (0 votes)
12 views

Error Handling in Python

The document shows an example of using try/except/finally blocks in Python. It has a loop that repeatedly prompts the user for input until valid numeric values are entered for age and salary, handling any errors in the try/except and running finally code after each attempt.

Uploaded by

gireshmittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Error Handling in Python

The document shows an example of using try/except/finally blocks in Python. It has a loop that repeatedly prompts the user for input until valid numeric values are entered for age and salary, handling any errors in the try/except and running finally code after each attempt.

Uploaded by

gireshmittal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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

You might also like