簡體   English   中英

在python中混淆異常行為

[英]confusing exception behavior in python

我有一些類似於這的django代碼(這是python 2.7.1)

try:
    a = some_model.objects.get(some_field='foo') #this could except if for some reason it doesn't exist
    method_that_throws_exception()    #it won't reach this if we get a DoesNotExist
except some_model.DoesNotExist:
    #if it doesn't exist create it then try again
    a = some_model.objects.create(....)
    try:
        method_that_throws_exception() #this time lets say another exception is raised by method
        print 'this should not print right?'
    except Exception as e:
        logging.error("error msg here")

問題是“這不應該打印”的行仍在打印中。 我很困惑。 我覺得我可能忽略了一些非常簡單的事情,但目前可能有一些隧道視野。 提前致謝。

更新:如果我刪除嵌套的try塊,則在調用拋出異常的方法下面的打印仍會打印。

我想通了,這個方法里面有一個嘗試塊,但我沒有提高。 在method_that_throws_exception()中的異常處理程序中添加raise修復了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM