pythonpython-3.xexceptionerror-handlingpython-tenacity

Python tenacity: How to retry if exception is NOT of a certain type?


How to retry a function if the exception is NOT of a certain type using Python's tenacity?

retry_if_exception_type will retry if there is risen an exception of a certain type. not does not seems to work placed before the method nor before its arguments.

retry_unless_exception_type, on the other side, loops forever, even if there is not risen error, until there is risen error of a certain type.


Solution

  • retry_if_not_exception_type is available since version 8.0.0

    Retries except an exception has been raised of one or more types.

    So if you use retry_if_not_exception_type((ValueError, OSError)), it will retry for any exception, except ValueError or OSError.