AGE = 10
while True:
age = input('>>: ').strip()
if age.isdigit(): # 只有在age为字符串形式的整数时,下列代码才不会出错,该条件是可预知的
age = int(age)
if age == AGE:
print('you got it')
break
try:
int('hello')
except IndexError as e: # 未捕获到异常,程序直接报错
print(str(e))
try:
int('hello')
except IndexError as e:
print('from IndexError: %s' % str(e))
except KeyError as e:
print('from KeyError: %s' % str(e))
except ValueError as e:
print('from ValueError: %s' % str(e))
except Exception as e:
print('from Exception: %s' % str(e))