Skip to content

Instantly share code, notes, and snippets.

@venkatsgithub1
Created June 11, 2017 05:19
Show Gist options
  • Save venkatsgithub1/3bce73505e7090edbed3790bb6ac3409 to your computer and use it in GitHub Desktop.
Save venkatsgithub1/3bce73505e7090edbed3790bb6ac3409 to your computer and use it in GitHub Desktop.
Basic Exception Handling in Python
def enterANumber ():
number=int(input ("Enter a number"))
print (number/2)
"""
Under try we write code.
Under except errors are handled.
In this example, user may enter
non numeric inputs which may
lead to ValueError.
The error is handled in except block as shown below.
If user enters a number such as 123, the method prints
123/2.
Else a value error is thrown and is caught by except
which again prompts user to enter the input.
"""
try:
enterANumber()
except ValueError:
print ("Not a Number")
enterANumber()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment