revisiting the "What am I running on?" question

Chris Angelico rosuav at gmail.com
Tue Feb 19 10:04:00 EST 2019


On Wed, Feb 20, 2019 at 1:59 AM songbird <songbird at anthive.com> wrote:
>
> MRAB wrote:
> ...
> > Don't use a bare except, it'll catch _any_ exception.
>
>   that's ok with this code IMO, but i see your point.
>

Not really, no. It means that ANY bug (barring an outright syntax
error) inside the try block will silently move you on to the next
check, possibly after printing out the message. Suppose you misspelled
"return".


    result = re.search("^/(tmp)|(var)|(usr)|(opt)|(home)", sysprobetmp)
    try:
        print ("Result : -->" + result.group(0) + "<--\n")

        retun ("posix")
    except:
        pass

It'll print out the result, bomb with a NameError, swallow the
NameError, and silently move on. The *only* times you should use a
bare except clause are with a "raise" inside it, or with a
log-and-return boundary marker (where "inner" code isn't allowed to
crash "outer" code, eg in a web app) - and the "log" part is
absolutely essential.

ChrisA



More information about the Python-list mailing list