如何通过错误代码处理twilio异常并进行处理

2024-06-10 02:25:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用以下方法来处理Python中的Twilio异常:

try:
    #code for sending the sms
    print(message.sid)
except TwilioRestException as e:
    print(e)

这允许我发送短消息,异常由Python处理

现在我需要“返回”异常代码来处理它们,比如,根据异常给用户一条消息

我怎样才能做到这一点


Tags: the方法messageforascodesmsprint
1条回答
网友
1楼 · 发布于 2024-06-10 02:25:35

如果引发异常不是一个选项,您可以简单地在except块下添加return

def func():
    # your logic
    try:
        #code for sending the sms
        print(message.sid)
    except TwilioRestException as e:
        print(e)
        return e.code  # or whetever attribute e has for it...

默认情况下,如果一切正常,函数将返回None。在客户端代码中

err = func()
if err:
    # sms wasn't sent, action required

相关问题 更多 >