【发布时间】:2021-10-04 09:03:32
【问题描述】:
我正在使用 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 可以忽略,因为这只是我遇到的错误。
【问题讨论】:
-
print将异常转换为str。 -
我很确定
for character in str(e):方法应该可以工作,因为从那时起你就可以保证迭代str而不是实际的Exception。你会得到什么错误信息?
标签: python python-3.x exception try-catch