Reputation: 1154
I have an error raise eb : list index out of range
.
I don't understand why when i do a raise in an other try - catch
I'm doing a try - catch
in a try - catch
and both raises errors.
Here is my code and the error line is at raise eb
:
try:
print("debut edit")
print(p)
modif_box = get_modif_box_profile(p)
post_box = get_Post_Box(p)
print("modi_box")
print(modif_box)
print("mbu id")
print(modif_box.id)
diff = {}
posts = {}
new_post = []
diff["posts"] = posts
posts["modified_post"] = new_post
for post in modif_box.edit_post_user.all():
# print(post.id_mod)
try:
messagenew = post_box.post.all().filter(id=post.id_mod)[0]
# print(post_new)
print("posts")
print(post)
# todo a factoriser
if messagenew.id > int(last_id) and messagenew.sender.id != p.id:
name = get_name_contact(p, messagenew)
return_post = {}
return_post["uid"] = messagenew.sender.id
return_post["pid"] = messagenew.id
return_post["author"] = name
return_post["title"] = messagenew.title
return_post["date"] = unix_time_millis(messagenew.date)
return_post["smile"] = count_smile(messagenew)
return_post["comment"] = count_comment(messagenew)
return_post["data"] = messagenew.data
return_post["type"] = messagenew.type_post.type_name
new_post.append(return_post)
else:
print("depop edit")
modif_box.edit_post_user.remove(post)
modif_box.save()
except Exception as eb:
PrintException()
# raise eb (if i decomment here i have an error in my program)
print(diff)
return diff
except Exception as e:
PrintException()
raise e
Regards and thanks
Upvotes: 1
Views: 291
Reputation: 160457
If you comment the raise
statement there, it doesn't mean that you don't have an Error; it simply means that you handled the Exception
-- which in your case is from what I can tell an IndexError
-- by catching it with the except Exception
and then calling PrintException()
.
When you raise
an exception what you actually do is:
The raise statement allows the programmer to force a specified exception to occur.
So, by un-commenting, you allow the IndexError
named eb
to re-appear after catching it in the inner try-except
block and get caught by the outer try - except
clause in which you again re-raise it.
Generally, you don't want to catch exceptions in such a generic way because it might hide some unpredicted behaviour of the program that you would like to know about.
Limit the exceptions you catch in the except clause by simply specifying them, in your case, an except clause of the form:
except IndexError as eb:
PrintException()
would probably suffice.
Upvotes: 2