Exception handling in python - raising exception - Voter age validation

# Raising exception 


try:

Name = input("Enter name : ")

Age = eval(input("Enter Age : "))

if(Age<0):

raise ValueError

except (ValueError):

print("Value Error: invalid Age value.")

except:

print("Unknown error.")

else:

if(Age>=18):

print("Valid age.", Name, "is eligible for voting.")

else:

print("Invalid age.",Name,"is not Eligible for voting.")

finally:

print("End of voter age validation.")


"""

Sample output

>python EH5.py

Enter name : Kelly

Enter Age : 21

Valid age. Kelly is eligible for voting.

End of voter age validation.


>python EH5.py

Enter name : Jerry

Enter Age : 12

Invalid age. Jerry  is not Eligible for voting.

End of voter age validation.


>python EH5.py

Enter name : Jose

Enter Age : -1

Value Error: invalid Age value.

End of voter age validation.


>python EH5.py

Enter name : Tom

Enter Age : 'a'

Unknown error.

End of voter age validation.


"""

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu