4 ms·
Anyone reading your code is going to assume this is a bug. The PEP is right that explicit is better than implicit. You should write `except BaseException` (whet
by williamsmj 2y ago
Anyone reading your code is going to assume this is a bug. The PEP is right that explicit is better than implicit. You should write `except BaseException` (whether or not this PEP is approved).
- TuxSH 2y ago"except:" is explicit enough and "except BaseException" is redundant. Moreover I think there is a real risk people are going to write "except Exception:" instead, which breaks in the fringe case an exception that derives from BaseException (enforced by interpreter) but not from Exception is thrown. Even if catch Exception is what users usually mean, changing code from "catch (BaseException):" to "catch Exception:" may break some code if improperly reviewed. It's also not worth breaking production code over this.
- williamsmj 2y ago> "except:" is explicit enough and "except BaseException" is redundant. Take that up with the consensus view of the python community, as reflected by python linters in their default configuration, almost all of which warn on bare except. The debate in the PEP is whether this should be a syntax error. The debate about whether it is good style is over though. > It's also not worth breaking production code over this. Agreed.
- TuxSH 2y ago> Take that up with the consensus view of the python community, as reflected by python linters in their default configuration, almost all of which warn on bare except. I don't fully agree on it being a purely a style issue (though the warnings from linters are wholly justified). From what I understand, "except:" is a code smell because you don't actually want to catch BaseException instead of just Exception. Linters wouldn't have warned about it if it meant "except Exception:". The real issue, IMO, is the fact non-exceptions (Ctrl-C, generator exit, program exit code, etc.) have been crammed into the exception system.
- zahlman 2y ago>which breaks in the fringe case an exception that derives from BaseException (enforced by interpreter) but not from Exception is thrown. For many users, in many cases, this would be fixing the code rather than breaking it. Forcing people to write either `except BaseException:` or `except Exception:` means forcing them to think about which one they actually mean. This is a good thing, just like the enforcement of proper separation between bytes and text is a good thing.
- rcxdude 2y agoOnly if they don't understand what 'raise' means. It's obvious this construct is just injecting some additional information in a passing exception, there's no issue if it catches everything.
- williamsmj 2y ago> It's obvious this construct is just injecting some additional information in a passing exception There is a good chance it will fail to do that. See elsewhere in this thread.
- theamk 2y agoIt will change exception class if logging function will fail... I wouldn't call this "good chance", those kinds of things are pretty unlikely in my experience.
- int_19h 2y agoBare except + reraise is a very common Python pattern, so no, it won't be assumed to be a bug. This was actually one of the major points in the discussion of the PEP that led to its rejection.