(1)代码:
#try/except
while True:
try :
x=int(input("Please Enter a number:"))
break
except ValueError:
print("Error!")
运行结果:
Please Enter a number:1
Please Enter a number:a
Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'a' is not defined
(2)代码:
#try-finally
while True:
try :
x=int(input("Please Enter a number:"))
break
except ValueError:
print("False!")
finally:
print ("OK!")
运行结果:
Please Enter a number:12
OK!