【发布时间】:2017-09-07 07:14:16
【问题描述】:
我正在使用 Twilio 的 API 来返回有关电话号码的信息。部分电话号码无效,返回错误如
Traceback (most recent call last):
File "test_twilio.py", line 17, in <module>
number = client.lookups.phone_numbers("(4154) 693-
6078").fetch(type="carrier")
File "/Users/jawnsano/anaconda/lib/python2.7/site-
packages/twilio/rest/lookups/v1/phone_number.py", line 158, in fetch
params=params,
File "/Users/jawnsano/anaconda/lib/python2.7/site-
packages/twilio/base/version.py", line 82, in fetch
raise self.exception(method, uri, response, 'Unable to fetch
record')
twilio.base.exceptions.TwilioRestException:
HTTP Error Your request was:
GET /PhoneNumbers/(4154) 693-6078
Twilio returned the following information:
Unable to fetch record: The requested resource /PhoneNumbers/(4154)
693-6078 was not found
More information may be available here:
https://www.twilio.com/docs/errors/20404
如果返回如上所示的错误,我想打印 'There is an error.'。但是,对于我的 if 语句,有没有办法让 Python 在一般存在回溯错误/错误时打印它?我认为可能有比设置更好的方法
if returned_value = (super long error message):
etc...
【问题讨论】:
-
你能分享你的代码以便更好地理解吗?
-
不返回错误信息。
标签: python exception error-handling