提高

2024-09-27 21:24:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个错误raise eb : list index out of range。在

我不明白为什么我在另一家公司加薪try - catch 我在一个try - catch中执行try - catch,这两个操作都会引发错误。在

这是我的代码,错误行位于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

致以问候和感谢


Tags: nameboxidnewreturndiffpostedit
1条回答
网友
1楼 · 发布于 2024-09-27 21:24:33

如果您对raise语句进行注释,并不意味着您不存在错误;它只是意味着您处理了Exception,在您的例子中,我可以用except Exception捕捉它,然后调用PrintException()来告诉{}。在

当您^{}出现异常时,您实际要做的是:

The raise statement allows the programmer to force a specified exception to occur.

因此,通过取消注释,您可以允许名为ebIndexError在内部try-except块中捕获后重新出现,并被外部的try - except子句捕获,在该子句中再次引发它。在


一般来说,您不希望以这种通用的方式捕捉异常,因为它可能会隐藏一些您希望了解的程序的不可预测行为。在

限制在except子句中捕获的异常,方法是在您的示例中,指定一个except子句,格式如下:

except IndexError as eb:
    PrintException() 

可能就够了。在

相关问题 更多 >

    热门问题