From c59514d1265b3d441f3953243c5a9a45f8b46cc4 Mon Sep 17 00:00:00 2001 From: Abdul Wasiq <153769517+Abdul-Wasiq@users.noreply.github.com> Date: Sat, 2 May 2026 22:50:36 +0500 Subject: [PATCH] docs: add try/except example to Errors and Exceptions section The Errors and Exceptions section listed all exceptions but had no code example showing how to actually handle them. Added a practical try/except example covering the most common exceptions. --- docs/user/quickstart.rst | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/user/quickstart.rst b/docs/user/quickstart.rst index cef9e088d8..4b9754bd43 100644 --- a/docs/user/quickstart.rst +++ b/docs/user/quickstart.rst @@ -568,6 +568,16 @@ If a request exceeds the configured number of maximum redirections, a All exceptions that Requests explicitly raises inherit from :exc:`requests.exceptions.RequestException`. ------------------------ - -Ready for more? Check out the :ref:`advanced ` section. +You can handle these exceptions using a ``try``/``except`` block:: + + >>> try: + ... r = requests.get('https://api.github.com/events', timeout=5) + ... r.raise_for_status() + ... except requests.exceptions.Timeout: + ... print("The request timed out.") + ... except requests.exceptions.ConnectionError: + ... print("A network problem occurred.") + ... except requests.exceptions.HTTPError as e: + ... print(f"HTTP error: {e}") + ... except requests.exceptions.RequestException as e: + ... print(f"Something went wrong: {e}")