简体   繁体   English

我的错误处理代码尝试/预期代码给我错误“在使用 try 和 except 语句赋值之前引用的局部变量”

[英]My error handling code try/expect code giving me error "local variable referenced before assignment with try and except statement"

I added this code here meant for error handling我在这里添加了这段代码,用于错误处理

try:
    music = session.query(Music).filter_by(Assetid=id).one()
    music.firstname = new_AssetName
    music.lastname = new_category
    music.DOB = new_borrower
    music.experience = new_status
    music.experience = new_value
    session.commit()
except Exception as e:
   message = "Error updating player:" + e.toString()  
finally:
    return template('success.tpl', message=message)

and it keeps on giving me the error它不断给我错误

UnboundLocalError: local variable 'message' referenced before assignment

My solution was to make message a global variable so like:我的解决方案是让 message 成为一个全局变量,如下所示:

except Exception as e:
   global message
   message = "Error updating player:" + e.toString() 

But this just gives me an error saying that message is not defined.但这只是给我一个错误,说消息未定义。 I am quite lost on how to fix this any help would be great我对如何解决这个问题很迷茫,任何帮助都会很棒

The message variable is not defined in your try block.消息变量未在您的try块中定义。 This means that when your code gets to the finally block, if the execution didn't raise an exception the message variable will be undefined and therefore raise the UnboundLocalError exception.这意味着当您的代码到达finally块时,如果执行没有引发异常,则消息变量将未定义,因此引发UnboundLocalError异常。

To solve this, you could simply do:要解决这个问题,您可以简单地执行以下操作:

try:
    music = session.query(Music).filter_by(Assetid=id).one()
    music.firstname = new_AssetName
    music.lastname = new_category
    music.DOB = new_borrower
    music.experience = new_status
    music.experience = new_value
    session.commit()
    message = "Successfully updated player"

except Exception as e:
    message = f"Error updating player: {e}"

finally:
    return template("success.tpl", message=message)

暂无
暂无

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

相关问题 Unbound Local Error:在try和except语句内进行赋值之前引用的局部变量 - Unbound Local Error: local variable referenced before assignment inside of a try and except statement 在我的代码上赋值错误之前引用的局部变量 - local variable referenced before assignment error on my code Python 3 UnboundLocalError:在try语句中赋值之前引用的局部变量 - Python 3 UnboundLocalError: local variable referenced before assignment in try statement 这个 python 代码有什么问题,它给出了这个错误: UnboundLocalError: local variable 'user' referenced before assignment - What is wrong with this python code, it is giving this error : UnboundLocalError: local variable 'user' referenced before assignment try and if - 赋值前引用的局部变量 - try and if - local variable referenced before assignment 错误代码:UnboundLocalError:赋值前引用的局部变量 - Error Code: UnboundLocalError: local variable referenced before assignment 下面的代码给出了一个 UnboundLocalError,分配前引用的局部变量 - the following code is giving an UnboundLocalError , Local variable referenced before assignment 当我在 try-except 块中使用该变量时,我在赋值之前引用了局部变量 - i am getting local variable referenced before assignment when i use that variable in try-except block Python - If 语句,在赋值之前引用错误局部变量“newfile” - Python - If statement, error local variable 'newfile' referenced before assignment Python:得到了“分配前引用的局部变量‘idx’”的错误语句 - Python: Got an Error Statement for "local variable 'idx' referenced before assignment"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM