Created
June 11, 2017 05:19
-
-
Save venkatsgithub1/3bce73505e7090edbed3790bb6ac3409 to your computer and use it in GitHub Desktop.
Basic Exception Handling in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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