简体   繁体   English

在try-except中递减时python不清楚行为

[英]python unclear behavior when decrementing in try-except

Is there any special behavior when decrementing a variable in the except clause? 在except子句中递减变量时是否有任何特殊行为? sid keeps incrementing until it first gets into the exception clause, then it just keeps the same value for the rest duration of the for loop. sid一直递增,直到它第一次进入exception子句为止,然后在for循环的其余时间内仅保持相同的值。

7 out of 105 tries throw an exception 105次尝试中有7次抛出异常

there is no printout "Fehlercode:", errorcode 没有打印输出“ Fehlercode:”,错误代码

Here's my code: 这是我的代码:

for bid in range(bidStart, bidEnd + 1):
    for syn in getSynsProBeitrag(bid):
        try:
            sid += 1
            query = "INSERT INTO zuord (bid, hid, sid) VALUES(%s, %s, %s)"
            cursor.execute(query, [bid, hid, sid])
            query2 = "INSERT INTO synonyme (synonym) VALUE (%s)"
            cursor.execute(query2, syn)

        except MySQLdb.IntegrityError, message:
            errorcode = message[0]      
            if errorcode == 1062:       
                sid -= 1
                print sid
            else:
                print "Fehlercode:", errorcode

solved: after the query2 throws it's first exception the first query is causing also an (the same) IntegrityError and then it just goes back and forth like colleen said 解决:在query2引发它的第一个异常后,第一个查询也引起了(相同的)IntegrityError,然后像colleen所说的那样来回往复

Well you're decrementing it in the except clause, and then incrementing it in the try, so it's just going back and forth.... if it fails the first time, it's going to keep failing. 好吧,您要在except子句中将其递减,然后在尝试中将其递增,因此它只是来回移动....如果第一次失败,它将一直失败。

eg 例如

try:
1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
try:
1+1=2 ->fail->id-1=1
....

etc. 等等

If you're trying to skip the id that failed, don't decrement it. 如果您尝试跳过失败的ID,请不要减少它。

There isn't any special behavior when decrementing in the except clause. 在except子句中递减时没有任何特殊行为。

Is it because the same exception keeps getting thrown so it keeps decrementing? 是否因为相同的异常不断抛出而使它不断递减? I notice you are only printing it out when you get the same errorcode, are you ever getting printouts for "Fehlercode: errorcode"? 我注意到您只有在得到相同的错误代码时才将其打印出来,您是否曾经获得过“ Fehlercode:错误代码”的打印输出?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM