我的 Telegram 机器人在遇到运行时错误后如何继续运行?

发布于 2025-01-20 02:48:26 字数 539 浏览 0 评论 0原文

普通脚本

假设我们正在编写一个基本的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇年纸鸢 2025-01-27 02:48:26

您正在寻找尝试/异性语句。在错误和异常

def f():
    ["a", "b", "c"][3] # this will error

try:
    f() 
    # The error will propagate up through function calls until it reaches
    # a try/except block (or there are no more functions, in which case your
    # program will stop and report the error as usual)
except IndexError as err:
    # This code will run if an IndexError happens. err will be a variable
    # with info on the error. Other errors will not be caught.
    print(err)
else:
    # This code will run only if there is no errors in the try: block.
    print("No errors!")
finally:
    # this code will run always at the end, no matter if an error occurred.
    print("Running finally block regardless")

You're looking for a try/except statement. Check out the python docs on Errors and Exceptions

def f():
    ["a", "b", "c"][3] # this will error

try:
    f() 
    # The error will propagate up through function calls until it reaches
    # a try/except block (or there are no more functions, in which case your
    # program will stop and report the error as usual)
except IndexError as err:
    # This code will run if an IndexError happens. err will be a variable
    # with info on the error. Other errors will not be caught.
    print(err)
else:
    # This code will run only if there is no errors in the try: block.
    print("No errors!")
finally:
    # this code will run always at the end, no matter if an error occurred.
    print("Running finally block regardless")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文