简体   繁体   中英

How to catch the orignial exception

I'm using the requests module with max_retries option. I would like to catch the exceptions only related to timeouts and slow replies:

import requests
from requests.exceptions import ConnectTimeout, Timeout

URL = 'http://exmaple.com/sleep' # sleeps for 5 seconds before reply

with requests.Session() as s:
    try:
        a = requests.adapters.HTTPAdapter(max_retries=2)
        s.mount('http://', a)
        r = s.get(URL, timeout=1)
    except (ConnectTimeout, Timeout) as err:
        print('# {} - timeout'.format(URL))

But it looks like the underlying urllib3 library throws ReadTimeoutError and requests doesn't catch it and throws ConnectionError instead:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='example.com', port=80): Max retries exceeded with url: /sleep (Caused by ReadTimeoutError("HTTPConnectionPool(host='example.com', port=80): Read timed out. (read timeout=1)"))

I don't want to add ConnectionError to the list because there are other exceptions that inherit from it so it would also catch those.

Is there a way to catch the original exception or perhaps all exceptions in the chain using traceback module.

Ideally, you should catch those other exceptions above ConnectionError and raise them if you want your program to throw an error.

class OtherException(requests.exceptions.ConnectionError):
    pass

try:
    raise OtherException('This is other exception.')
except OtherException as oe:
    raise oe
except requests.exceptions.ConnectionError:
    print('The error you want to catch')

You can use a similar contruct:

import traceback
import logging

try:
    whatever()
except Exception as e:
    logging.error(traceback.format_exc())
    # Your actions here

This will almost catch everything except, for example, KeyboardInterrupt and SystemExit.

Catching those would make the script quite hard to quit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM