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

Python 如何通过错误代码处理twilio异常并对其进行处理,python,exception,twilio,Python,Exception,Twilio,我使用以下方法处理Python中的Twilio异常: try: #code for sending the sms print(message.sid) except TwilioRestException as e: print(e) 这允许我发送sms,异常由Python处理 现在我需要“返回”异常代码以便处理它们,比如说,根据异常给用户一条消息 如何实现这一点?如果引发异常不是一个选项,您可以简单地在except块下添加return def func():

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

try:
    #code for sending the sms
    print(message.sid)
except TwilioRestException as e:
    print(e)
这允许我发送sms,异常由Python处理

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


如何实现这一点?

如果引发异常不是一个选项,您可以简单地在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

若引发异常不是一个选项,那个么您可以简单地在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