How to find out what type of exception to catch in python?

The general exception trap 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 name of the exception so that I can add the highlighted catch to it. How can I find him?

Is there a type function in the thrown exception, or can it be found in the lib metal source - available here

+4
source share
2 answers

The exception that occurs in the paste is foursquare.RateLimitExceeded (as the last line says). You should catch it as usual, or catch its base class foursquare.FoursquareException if you want to handle all errors from the module.

The code that throws the exception is simply looking for which class of exceptions to raise from the dictionary. This should not affect how you catch these errors.

+2
source

It was originally a comment, but since it received a lot of reproaches, and the OP claims that this is what they were looking for, I am posting it as an answer:

This seems to be a RateLimitExceeded exception. Although, if you really want to be sure, you can do this:

 try: # code except Exception as e: print e.__class__ 

This will print the exception class that was raised, which will give you the final answer

+2
source

Source: https://habr.com/ru/post/1444543/


All Articles