I've been trying to add an error handling mechanism to my code section. However when it runs it says 'continue outside of loop' but looking at the code it should be inside the try loop. What's going wrong?
def download_media_item(self, entry):
try:
url, path = entry
# Get the file extension example: ".jpg"
ext = url[url.rfind('.'):]
if not os.path.isfile(path + ext):
r = requests.get(url, headers=headers, timeout=15)
if r.status_code == 200:
open(path + ext, 'wb').write(r.content)
self.user_log.info('File {} downloaded from {}'.format(path, url))
return True
elif r.status_code == 443:
print('------------the server reported a 443 error-----------')
return False
else:
self.user_log.info('File {} already exists. URL: {}'.format(path, url))
return False
except requests.ConnectionError:
print("Received ConnectionError. Retrying...")
continue
except requests.exceptions.ReadTimeout:
print("Received ReadTimeout. Retrying...")
continue
forloop.continuestatements are not inside any loop. Are you saying that there is aforloop somewhere?forloop? Both of thecontinuestatements in yourexceptcases are outside of a loopcontinueto continue execution following thetrystatement; that already happens. For example, omittingcontinuefromexcept requests.ConnectionErrordoes not cause theexcept requests.exceptions.ReadTimeoutblock to execute next.try loop.