我的 Telegram 机器人在遇到运行时错误后如何继续运行?
普通脚本
假设我们正在编写一个基本的 Python 程序。
numbers = [1,2,3]
special_number = numbers[3]
print(special_number)
这段代码不起作用,因为它给出了索引超出范围的错误,因为只有 3 个索引而不是 4 个。如果我们不重新运行它,程序将停止并且无法工作。
Telegram Bot 脚本
然而,当我们创建 Telegram 机器人时,这种情况不会发生。
def foo(update: Update, context: CallbackContext):
numbers = [1,2,3]
special_number = numbers[3]
print(special_number)
当程序运行 foo 函数时,它会给出错误但不会停止。如果它是每 10 分钟重复一次的作业,调度程序总是调用该函数并给出错误。此外,用户也可以以某种方式触发该功能。我很好奇这背后的原因是什么。
Normal Script
Let's say we're writing a basic python program.
numbers = [1,2,3]
special_number = numbers[3]
print(special_number)
This code doesn't work, because it gives index out range error due to there are only 3 indexes not 4. Program will stop and won't work if we do not rerun it.
Telegram Bot Script
However when we are creating a Telegram bot, this situation doesn't happen.
def foo(update: Update, context: CallbackContext):
numbers = [1,2,3]
special_number = numbers[3]
print(special_number)
When the program runs the foo function, it gives error but doesn't stop. If it's a job that is repeated in every 10 minutes, Scheduler always call that function and gives error. Also, users can somehow trigger the function too. I'm curious about what is the reason behind this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找尝试/异性语句。在错误和异常
You're looking for a try/except statement. Check out the python docs on Errors and Exceptions