00 异常处理 p185

jhchena / 2024-09-27 / 原文

4.5 异常处理 p185

try:
    val = input('请输入数字:')
    num = int(val)
except Exception as e:
    print('操作异常')
# import requests
#
# try:
#     ret = requests.get('http://www.google.com')
#     print(ret.text)
# except Exception as e:
#     print('请求异常')
def func(a):
    try:
        return a.strip()
    except Exception as e:
        pass
    return False

v = func('alex')
if not v:
    print('函数执行失败')
else:
    print('结果是',v)