[Python] Python - Conditionally Catching Exceptions

Discussão em 'Python' iniciado por Stack, Agosto 1, 2025.

  1. Stack

    Stack Membro Participativo

    Is it possible to conditionally handle exceptions? I would like to be able to write a function so that the caller can decide who handles the exception.

    Something like this:

    def my_func(my_arg, handle_exceptions):
    try:
    do_something(my_arg)
    except Exception as e if handle_exceptions:
    print("my_func is handling the exception")

    Continue reading...

Compartilhe esta Página