On Fri, Sep 25, 2020 at 3:38 PM Steven D'Aprano <st...@pearwood.info> wrote:
> The last thing I want to see is people being encouraged to write code
> like this:
>
>     def demo(arg)->Something:
>         # Raises ValueError
>         try:
>             processing...
>         except ValueError:
>             raise
>         except:
>             # Any other unexpected error.
>             raise ValueError('something unexpected')
>
>
> just to ensure that the declared exception is correct.
>

Another thing I don't want to see is:

def demo(arg):
    try:
        some_func() # declared to raise ValueError, TypeError, and TimeoutError
        return 42
    except ValueError:
        return 0
    except TypeError
        # Shouldn't happen, just return whatever
        return -1
    except TimeoutError
        # I have no idea why this would happen! Just return zero and hope
        # for the best. TODO: Figure out a better return value.
        return 0

where people feel they HAVE to catch everything that a function could
raise. (Which, btw, still ignores the fact that basically any code
could raise MemoryError, KeyboardInterrupt, etc.)

Only catch those exceptions that you can actually handle. Otherwise,
it's irrelevant what the function might raise.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WC2SRZ64HSS4JUXEQTPCPIT2R62ZNNAT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to