python 请求:我怎样才能得到 "exception code"
python requests: how can I get the "exception code"
我正在使用 "requests (2.5.1)" 。现在我想捕获异常和 return 带有一些异常消息的字典,我将 return 的字典如下:
{
"status_code": 61, # exception code,
"msg": "error msg",
}
但是现在我无法得到错误status_code和错误信息,我尝试使用
try:
.....
except requests.exceptions.ConnectionError as e:
response={
u'status_code':4040,
u'errno': e.errno,
u'message': (e.message.reason),
u'strerror': e.strerror,
u'response':e.response,
}
但是它太冗余了,我怎样才能让错误信息简单化?任何人都可以提供一些想法?
try:
#the codes that you want to catch errors goes here
except:
print ("an error occured.")
这将捕获所有错误,但最好定义错误而不是捕获所有错误并打印 errors.Like;
的特殊句子
try:
#the codes that you want to catch errors goes here
except SyntaxError:
print ("SyntaxError occured.")
except ValueError:
print ("ValueError occured.")
except:
print ("another error occured.")
或者;
try:
#the codes that you want to catch errors goes here
except Exception as t:
print ("{} occured.".format(t))
我正在使用 "requests (2.5.1)" 。现在我想捕获异常和 return 带有一些异常消息的字典,我将 return 的字典如下:
{
"status_code": 61, # exception code,
"msg": "error msg",
}
但是现在我无法得到错误status_code和错误信息,我尝试使用
try:
.....
except requests.exceptions.ConnectionError as e:
response={
u'status_code':4040,
u'errno': e.errno,
u'message': (e.message.reason),
u'strerror': e.strerror,
u'response':e.response,
}
但是它太冗余了,我怎样才能让错误信息简单化?任何人都可以提供一些想法?
try:
#the codes that you want to catch errors goes here
except:
print ("an error occured.")
这将捕获所有错误,但最好定义错误而不是捕获所有错误并打印 errors.Like;
的特殊句子try:
#the codes that you want to catch errors goes here
except SyntaxError:
print ("SyntaxError occured.")
except ValueError:
print ("ValueError occured.")
except:
print ("another error occured.")
或者;
try:
#the codes that you want to catch errors goes here
except Exception as t:
print ("{} occured.".format(t))