0

This is part of a script that iterates over our current ecommerce products and then checks stock at the distributor. This code is in a for loop, which is why the end of the except statement has "continue". I'm getting the following error intermittently:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 

I thought I would be able to catch the error and continue the for loop. What am I doing wrong? I was under the impression that except requests.exceptions.RequestException as _error: would catch all errors.

Here is the code:

try: print("Line 85") Check_BC_API_CALLS() print("Line 87") r2 = requests.get('https://api.bigcommerce.com/stores/' + _BcStoreHash + '/v3/catalog/products?page=' + str(_CurrentPage) + '&limit=1', headers=_BcHeaders) print("Line 90") except requests.exceptions.RequestException as _error: print("BigCommerce request failed: " + _error) Check_BC_API_CALLS() continue 
4
  • 1
    You can try to catch TimeoutError in you except. Commented Jun 23, 2022 at 14:21
  • Is it best practice to add an exception for each type of error requests can throw? Commented Jun 23, 2022 at 14:24
  • Yes, because depending on the type of exception you could act or respond in a different way. Commented Jun 23, 2022 at 14:29
  • @Texas_Technician Its only best practice if you are planning to do something about the exception, no point in catching all kinds of errors in their own blocks just to print the msg, for example. Are you trying to do something about the error after you catch it, or do you just want to ignore it and keep the loop running? Commented Jun 23, 2022 at 18:04

1 Answer 1

1

Python exception classes hierarchy is:

IOError that is a base class for RequestException have been merged with OSError after python version 3.3:

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

So as you can see RequestException is not a parent class of TimeoutError exception and can't be used to catch this type of errors:

import requests try: raise TimeoutError('TIMEOUT ERROR') except requests.exceptions.RequestException as e: # catch RequestException type errors that are specific for request library only # do something print("RequestExceptions will be caught") except TimeoutError as e: # catch TimeoutError type errors that has same level in hierarchy as RequestException errors # do something print("TimeoutErrors will be caught") except OSError as e: # catch all OSError type errors. Little bit wider than previous exceptions # do something print("TimeoutErrors or RequestExceptions or any other OSErrors will be caught") except Exception as e: # catch any python errors, because all errors in python are children (sub types) for Exception class. Most wider exception type # do something print("TimeoutErrors or RequestExceptions or OSErrors or any other python errors will be caught") 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.