1. Junior

    How do you choose the right exception and deal with the exception hierarchy?

    Raise an exception that precisely expresses the cause of the failure (such as ValueError, KeyError or a domain-specific exception), and catch the narrowest type you can actually handle instead of except Exception everywhere.

    Read the full explanation
  2. Junior

    What is the difference between except, else and finally in a try statement?

    except handles the matching exception, else runs only when the try block succeeds without an exception, and finally always runs whether the operation succeeded or failed.

    Read the full explanation
  3. Junior

    When should you create a custom exception?

    When the caller needs to distinguish a specific failure in the domain and make a different decision based on it, or when you need to attach structured data to the error.

    Read the full explanation
  4. Mid-level

    What is the difference between the EAFP and LBYL styles?

    EAFP (easier to ask forgiveness than permission) tries the operation directly and handles the exception, while LBYL (look before you leap) checks conditions before acting.

    Read the full explanation
  5. Mid-level

    What is raise ... from for?

    It links the new exception to the original cause through __cause__, so the traceback shows the full chain with the phrase "The above exception was the direct cause...", which preserves diagnostic context.

    Read the full explanation
  6. Mid-level

    How does contextlib help with resource management?

    It provides tools to build and combine context managers: contextmanager to write a manager as a generator, ExitStack to manage a variable number of resources, suppress to ignore a specific exception, plus closing and nullcontext.

    Read the full explanation
  7. Mid-level

    How do you log an exception without losing the traceback?

    Use logger.exception() inside an except block (it logs at ERROR level with the traceback), or pass exc_info=True at any other level, instead of logging only str(err).

    Read the full explanation
  8. Senior

    How do you design timeouts and retries without multiplying the damage?

    Set a timeout on every I/O operation and retry only on transient errors, with exponential backoff, jitter and a maximum number of attempts, and only for operations that are safe to repeat (idempotent).

    Read the full explanation
  9. Senior

    When do you need ExceptionGroup and except*?

    When several concurrent operations fail at the same time (such as TaskGroup tasks) and you need to show every error and handle each type, instead of settling for the first error.

    Read the full explanation
  10. Mid-level

    Where should validation of data coming from outside the system be placed?

    At trust boundaries: HTTP requests, CLI arguments, files, environment variables and queue messages. Convert the data there into a trusted model (parse, don't validate) and then pass it inward.

    Read the full explanation