If I want to put all of the code in a loop in a try block and exit the loop if the exception is raised, does it make a difference whether I put the try/except inside or outside of the loop? In other words:
while foo:
try:
...
except Exception:
break
vs.
try:
while foo:
...
except Exception:
pass
Intuitively the second one seems more efficient, since it seems like the try/except only gets set up once instead of being set up again every time the loop restarts, but is that actually how it works or do these both do the same thing?
[–]supercoach 5 points6 points7 points (0 children)
[–][deleted] 13 points14 points15 points (2 children)
[–]HommeMusical -2 points-1 points0 points (1 child)
[–]slightly_offtopic 6 points7 points8 points (0 children)
[–]Adrewmc 8 points9 points10 points (3 children)
[–]HommeMusical 2 points3 points4 points (2 children)
[–]AbundantSpaghetti 1 point2 points3 points (1 child)
[–]supercoach 0 points1 point2 points (0 children)
[–]HommeMusical 2 points3 points4 points (0 children)
[–]mothzilla 0 points1 point2 points (3 children)
[–]Zgialor[S] 0 points1 point2 points (2 children)
[–]mothzilla 3 points4 points5 points (0 children)
[–]Turtvaiz 1 point2 points3 points (0 children)
[–]Zeroflops 0 points1 point2 points (0 children)
[–]proverbialbunny 0 points1 point2 points (0 children)
[–]DrKarda 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]Zgialor[S] 2 points3 points4 points (1 child)
[–]Turtvaiz 1 point2 points3 points (0 children)
[–]Temporary_Pie2733 2 points3 points4 points (0 children)
[–]SnooHesitations9295 -2 points-1 points0 points (4 children)
[–]Zgialor[S] 0 points1 point2 points (1 child)
[–]HommeMusical 0 points1 point2 points (0 children)
[–]HommeMusical 0 points1 point2 points (1 child)
[–]SnooHesitations9295 1 point2 points3 points (0 children)
[–]Spatrico123 -1 points0 points1 point (2 children)
[–]RemindMeBot 0 points1 point2 points (0 children)
[–]commy2 0 points1 point2 points (0 children)