[英]How to use exception handling in a loop (for n iterations) in python so that results are stored in a file but loop doesn't terminate?
我想以文件中的日志形式引發異常,並且如果異常在特定迭代中發生,則循環不應終止。
輸入:
str = 'my name and age are not known'
for i in range(0,n):
try:
list =['name','age']
for word in list:
if word in str:
print('output verified')
else:
raise Exception('unmatched values')
print('unmatched values for iteration n')
except Exception as ex:
TestCase.status = FAIL
raise ex
輸出:
unmatched values
我希望將輸出存儲在文件中,並且循環應繼續進行下一次迭代
看一下下面的代碼,可以按照要求在文件中記錄日志:
for i in range(0,n):
try:
list =['name','age']
for word in list:
if word in str:
print('output verified')
else:
# instead of a generic exception a specific exception can be raised
raise Exception('unmatched values')
print('unmatched values for iteration n')
except Exception:
TestCase.status = FAIL
print "Log you result in a file "
# raise ex - need not be here
快樂編碼:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.