Use else statement to execute a block of code when everything went well
What to do when everything went well
We can use else statement to execute a block of code when everything went well.
To have a block of code that is executed unless something bad happens, you can add an else clause:
try: # w w w.j a v a2s .co m
print 'A simple task'
except:
print 'What? Something went wrong?'
else:
print 'Ah...It went as planned.'
while 1:
try:
x = input('Enter the first number: ')
y = input('Enter the second number: ')
value = x/y
print 'x/y is', value
except:
print 'Invalid input. Please try again.'
else:
break
The code above generates the following result.