I'm currently using a broad Exception handler in my code and I would like to remove this broad exception. Which exceptions should I handle? Are these exceptions generated in my function "func" or which ones? I would like to list them.

Using this page as reference:

# Retrieve a single page and report the url and contents
def load_url(url, timeout):
    if not url:
       raise ValueError('Invalid URL')
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()    

with futures.ThreadPoolExecutor(max_workers=_MAX_WORKERS) as executor:
        # Start the load operations and mark each future with its URL            
        future_to_url = {
              executor.submit(load_url, _EXECUTOR_TIMEOUT): image
              for image in image_list
        for future in futures.as_completed(future_to_url):
          image = future_to_url[future]
            images.append((image.master_id, future.result()))
          except Exception as exc: 
            logging.exception('%r Generated an exception: %s', image.url, exc)
  • \$\begingroup\$ How can we possibly answer this if you don't show us the code for func? \$\endgroup\$ Jul 29, 2017 at 9:10
  • \$\begingroup\$ Hi Gareth, I just edited the original post, thanks for replying \$\endgroup\$
    – gogasca
    Jul 29, 2017 at 23:52

1 Answer 1


The docs for urllib.request.urlopen suggest that HTTPError & URLError will typically be raised, in addition to your ValueError. Other rare ones are possible so sticking with Exception, as you have it, seems reasonable.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.