之前有人问过这个问题,但答案是建议print
哪个不好。
鉴于这种:
class BitbucketError(Exception):
pass
try:
response = requests.get('https://bitbucket.org/.......')
return response.json()['data']
# because JSONDecoderError inherits from ValueError
except (json.JSONDecoderError, ValueError) as e:
raise BitbucketError(response.status_code)
没关系。但是如果我要在另一个项目中使用这个模块,我会在哪里
# save to dabase, example 1
raw_code = mymodule.bitbucket.file(...)
db.save(raw_code)
# return json back to user (api server, example 2)
raw_code = mymodule.bitbucket.file(...)
return {'data': raw_code}
我确定我想在上面的代码段中捕获异常。但问题是,我应该让原始的传播或自己定义一个例外吗?
我正在尝试了解如何正确处理错误,因为错误可能是由于 404、405 甚至 500 造成的。
希望这个问题有意义。