如何知道在python中捕获哪种异常类型? [英] How to know which exception type to catch in python?

查看:275
本文介绍了如何知道在python中捕获哪种异常类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常规异常捕获器记录以下异常:

A general exception catcher logs the following exception:

> Traceback (most recent call last):   File "4sq.py", line 37, in
> <module>
>     checkin = client.checkins()   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 416, in __call__
>     return self.GET('{CHECKIN_ID}'.format(CHECKIN_ID=CHECKIN_ID), params, multi=multi)   File
> "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 217, in GET
>     return self.requester.GET(self._expanded_path(path), *args, **kwargs)   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 163, in GET
>     return self._request(url)   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 200, in _request
>     return _request_with_retry(url, data)['response']   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 696, in _request_with_retry
>     return _process_request_with_httplib2(url, data)   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 719, in _process_request_with_httplib2
>     return _check_response(data)   File "/usr/local/lib/python2.7/dist-packages/foursquare-20120716-py2.7.egg/foursquare/__init__.py",
> line 742, in _check_response
>     raise exc(meta.get('errorDetail')) RateLimitExceeded: Quota exceeded

我想知道特定的异常名称,因此可以为其添加专用的捕获. 怎么找到?

I would like to know the specific exception name so I could add a dedicated catch to it. How can it be found?

在捕获的异常上是否存在类型"函数,还是应该在引发库的源代码中找到它?可用的此处

Is there a 'type' function on the caught exception or should it be found in the throwing lib's source - available here

推荐答案

粘贴中引发的异常是foursquare.RateLimitExceeded(如最后一行所述).如果您要处理模块中的所有错误,则应该能够正常捕获它,或者捕获其基类foursquare.FoursquareException.

The exception being raised in the paste is foursquare.RateLimitExceeded (as it says in the final line). You should be able to catch it as normal, or catch its base class foursquare.FoursquareException if you want to handle all errors from the module.

引发异常的代码只是从字典中查找要引发的异常类.这对您捕获这些错误的方式没有任何影响.

The code that raises the exception is just looking up which exception class to raise from a dictionary. That shouldn't have any effect on how you catch those errors.

这篇关于如何知道在python中捕获哪种异常类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆