有这样一个“迭代器”:
class MyIterator():
def __init__(self, lst):
self.lst = lst
self.counter = 0
def __next__(self):
try:
z = self.lst[self.counter]
self.counter += 2
return z
except:
raise StopIteration
出现错误后,它会输出以下内容:
Traceback (most recent call last):
File "C:/BookBot/test.py", line 10, in __next__
z = self.lst[self.counter]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/BookBot/test.py", line 22, in <module>
print(next(x))
File "C:/BookBot/test.py", line 14, in __next__
raise StopIteration
StopIteration
问题:如何做到这一点,只有StopIteration,有可能做到这一点吗?
你在上面捕捉到这个异常并在那里输出你想要的任何东西。以及如何准确地“清除”我尚未找到
Exception的当前状态:Python结论:
默认情况下,运行时本身会输出整个异常跟踪链,除非您已经捕获到异常。