1
我想用这个函数测试多个日期的格式,然后在所有检查完成后使用sys.exit(1)退出,如果其中任何一个返回错误。如果多次检查中有任何一次发生错误,我该如何返回?如果在函数的try/except块中发现错误
def test_date_format(date_string):
try:
datetime.strptime(date_string, '%Y%m')
except ValueError:
logger.error()
test_date_format("201701")
test_date_format("201702")
test_date_format("201799")
# if any of the three tests had error, sys.exit(1)
除了在函数调用外传播异常吗? –