在 for 循环期间引发异常并在 python 中的下一个索引处继续
Raising an exception during for loop and continuing at next index in python
我整天都在伤脑筋,我似乎无法解决这个问题。我应该写一个异常 class 然后在迭代期间引发它并从我离开的地方继续。
class OhNoNotTrueException(Exception):
"""Exception raised when False is encountered
Attributes:
message -- explanation of the error"""
def __init__(self, value):
self.value = value
am_i_true_or_false = [True, None, False, "True", 0, "", 8, "False", "True", "0.0"]
try:
for i in am_i_true_or_false:
if i is False:
raise OhNoNotTrueException(i)
continue #<--this continue does not work
else:
print(i, "is True")
except OhNoNotTrueException as e:
print(e.value, "is False")
但是,即使在继续之后,我也无法将迭代返回到最后一个索引。我不确定这是否是唯一的方法,但我在这里打破了我的头。有人想尝试一下吗?
我应该得到以下输出:
确实如此。
None 为假
假的就是假的
确实如此。
0 为假
为假
8 为真。
假的是真的。
确实如此。
0.0 为真。
您应该在 try/except 块之外执行循环。然后一个单独的列表条目将被 try catch 检查,然后循环将继续下一个:
for i in am_i_true_or_false:
try:
if i is False:
raise OhNoNotTrueException(i)
else:
print("{} is True".format(i))
except OhNoNotTrueException as e:
print("{} is False".format(e.value))
你这样做的方式是,循环一直执行到第一个异常,然后执行 except 块,程序结束。 continue
在这种情况下未达到,因为您抛出了一个异常,该异常将在 except 块中被捕获。
抛出异常后的一切都将永远无法到达,你将被带到循环之外,就好像try-block中的一切从未发生过一样。因此,您需要在循环内 try/except:
In [5]: for i in am_i_true_or_false:
...: try:
...: if i is False:
...: raise OhNoNotTrueException(i)
...: else:
...: print(i, "is not False")
...: except OhNoNotTrueException as e:
...: print(e.value, "is False")
...:
True is not False
None is not False
False is False
True is not False
0 is not False
is not False
8 is not False
False is not False
True is not False
0.0 is not False
注意如果您的 try 块包含循环会发生什么:
In [2]: try:
...: for i in am_i_true_or_false:
...: if i is False:
...: raise Exception()
...: else:
...: print(i,"is not False")
...: except Exception as e:
...: continue
...:
File "<ipython-input-2-97971e491461>", line 8
continue
^
SyntaxError: 'continue' not properly in loop
我整天都在伤脑筋,我似乎无法解决这个问题。我应该写一个异常 class 然后在迭代期间引发它并从我离开的地方继续。
class OhNoNotTrueException(Exception):
"""Exception raised when False is encountered
Attributes:
message -- explanation of the error"""
def __init__(self, value):
self.value = value
am_i_true_or_false = [True, None, False, "True", 0, "", 8, "False", "True", "0.0"]
try:
for i in am_i_true_or_false:
if i is False:
raise OhNoNotTrueException(i)
continue #<--this continue does not work
else:
print(i, "is True")
except OhNoNotTrueException as e:
print(e.value, "is False")
但是,即使在继续之后,我也无法将迭代返回到最后一个索引。我不确定这是否是唯一的方法,但我在这里打破了我的头。有人想尝试一下吗?
我应该得到以下输出:
确实如此。
None 为假
假的就是假的
确实如此。
0 为假
为假
8 为真。
假的是真的。
确实如此。
0.0 为真。
您应该在 try/except 块之外执行循环。然后一个单独的列表条目将被 try catch 检查,然后循环将继续下一个:
for i in am_i_true_or_false:
try:
if i is False:
raise OhNoNotTrueException(i)
else:
print("{} is True".format(i))
except OhNoNotTrueException as e:
print("{} is False".format(e.value))
你这样做的方式是,循环一直执行到第一个异常,然后执行 except 块,程序结束。 continue
在这种情况下未达到,因为您抛出了一个异常,该异常将在 except 块中被捕获。
抛出异常后的一切都将永远无法到达,你将被带到循环之外,就好像try-block中的一切从未发生过一样。因此,您需要在循环内 try/except:
In [5]: for i in am_i_true_or_false:
...: try:
...: if i is False:
...: raise OhNoNotTrueException(i)
...: else:
...: print(i, "is not False")
...: except OhNoNotTrueException as e:
...: print(e.value, "is False")
...:
True is not False
None is not False
False is False
True is not False
0 is not False
is not False
8 is not False
False is not False
True is not False
0.0 is not False
注意如果您的 try 块包含循环会发生什么:
In [2]: try:
...: for i in am_i_true_or_false:
...: if i is False:
...: raise Exception()
...: else:
...: print(i,"is not False")
...: except Exception as e:
...: continue
...:
File "<ipython-input-2-97971e491461>", line 8
continue
^
SyntaxError: 'continue' not properly in loop