2015-10-27 65 views
1

我有錯誤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 

的問候和感謝

+0

請包括您的堆棧跟蹤 – CrakC

回答

2

如果你發表意見raise聲明那裏,它並不意味着你有錯誤;它只是意味着你處理Exception - 而你的情況是什麼,我可以告訴一個IndexError - 通過與except Exception捕獲它,然後調用PrintException()

當你raise異常你真正要做的是:

raise語句允許程序員強制發生指定的異常。

所以,通過取消註釋,你讓IndexError命名eb在內部try-except塊捕獲它之後重新出現並獲得通過外try - except子句中,你又重新提出來捕獲。


一般情況下,你不希望捕捉異常在這樣一種通用的方法,因爲它可能隱藏你想了解該計劃的一些不可預測的行爲。

限制,只需指定它們,你的情況,除了一種形式的條款,你在趕除了條款的例外情況:

except IndexError as eb: 
    PrintException() 

可能就足夠了。