尝试遍历异常时出现错误
Given error when trying to iterate through an exception
我正在使用 PRAW 制作 Reddit 机器人,代码给出了异常不可迭代的错误。这是我的代码的简化:
try:
#something with praw that isn't relevant
except Exception as e: #this except error catches the APIexception, APIexpections in PRAW are a wide field of exceptions that dont't always have the same solution, so I scan the text for the error I'm looking for.
print(e)
if "keyword thats in the error" in e:
#fix the problem with the specific error
else:
print("Unkown APIExpection error")
这对我来说很好用,但是当我 运行 这个代码时:
try:
#something
except Exception as e:
for character in e:
print(character)
#I also tried this
try:
#something
except Exception as e:
for character in str(e):
print(character)
#None of the above work but this is my actual code and what I need to do, anything that gets the above to work should work here too, I'm just letting you know this so that I don't get any other errors I have to ask another question for.
try:
#something
except Exception as e:
characterNum = 0
for character in e:
characterNum += 1
print(str(characterNum) + ": " + character)
它给了我一个“TypeError:'RedditAPIException' is not iterable”,RedditAPIException 可以忽略,因为这只是我正在捕获的错误。
将异常转换为字符串,然后检查 if 语句。
更改为 => if "keyword thats in the error" in str(e):
我正在使用 PRAW 制作 Reddit 机器人,代码给出了异常不可迭代的错误。这是我的代码的简化:
try:
#something with praw that isn't relevant
except Exception as e: #this except error catches the APIexception, APIexpections in PRAW are a wide field of exceptions that dont't always have the same solution, so I scan the text for the error I'm looking for.
print(e)
if "keyword thats in the error" in e:
#fix the problem with the specific error
else:
print("Unkown APIExpection error")
这对我来说很好用,但是当我 运行 这个代码时:
try:
#something
except Exception as e:
for character in e:
print(character)
#I also tried this
try:
#something
except Exception as e:
for character in str(e):
print(character)
#None of the above work but this is my actual code and what I need to do, anything that gets the above to work should work here too, I'm just letting you know this so that I don't get any other errors I have to ask another question for.
try:
#something
except Exception as e:
characterNum = 0
for character in e:
characterNum += 1
print(str(characterNum) + ": " + character)
它给了我一个“TypeError:'RedditAPIException' is not iterable”,RedditAPIException 可以忽略,因为这只是我正在捕获的错误。
将异常转换为字符串,然后检查 if 语句。
更改为 => if "keyword thats in the error" in str(e):