How to prevent try catching every possible line in python?

前端 未结 6 1766
遥遥无期
遥遥无期 2020-12-14 02:42

I got many lines in a row which may throw an exception, but no matter what, it should still continue the next line. How to do this without individually try catching every si

6条回答
  •  攒了一身酷
    2020-12-14 02:59

    Apart from the answers provided, I think its worth to note that one-line try-except statements have been proposed - see the related PEP 463 with the unfortunate rejection notice:

    """ I want to reject this PEP. I think the proposed syntax is acceptable given the
    desired semantics, although it's still a bit jarring. It's probably no worse than the
    colon used with lambda (which echoes the colon used in a def just like the colon here
    echoes the one in a try/except) and definitely better than the alternatives listed.
    
    But the thing I can't get behind are the motivation and rationale. I don't think that
    e.g. dict.get() would be unnecessary once we have except expressions, and I disagree 
    with the position that EAFP is better than LBYL, or "generally recommended" by Python. 
    (Where do you get that? From the same sources that are so obsessed with DRY they'd rather
    introduce a higher-order-function than repeat one line of code? :-)
    
    This is probably the most you can get out of me as far as a pronouncement. Given that
    the language summit is coming up I'd be happy to dive deeper in my reasons for rejecting
    it there (if there's demand).
    
    I do think that (apart from never explaining those dreadful acronyms :-) this was a 
    well-written and well-researched PEP, and I think you've done a great job moderating the 
    discussion, collecting objections, reviewing alternatives, and everything else that is 
    required to turn a heated debate into a PEP. Well done Chris (and everyone who 
    helped), and good luck with your next PEP! """
    

提交回复
热议问题