9

So I am aware you can use try/except blocks to manipulate the output of errors, like so:

try:
 print("ok")
 print(str.translate)
 print(str.foo)
except AttributeError:
 print("oops, found an error")
print("done")

...which gives the following output:

ok
<method 'translate' of 'str' objects>
oops, found an error
done

Now, is there a way to do the following with a while loop, like while not AttributeError, like this:

while not AttributeError:
 print("ok")
 print(str.translate)
 print(str.foo)
print("done")

which would give the same output as above, just without oops, found an error? This would reduce the need for except: pass type blocks, which are necessary but a little pointless if you have nothing to do in the except block.

I tried while not AttributeError and while not AttributeError(), which both just completely skip anything in the while block. So, is there a way to do this in Python?

Edit: This really isn't a loop per se, but the while block would run, and continue on if it encounters an error, and just continue on if it reaches the end.

asked Dec 29, 2018 at 17:04
6
  • You must see try / except blocks as "if everything is ok" / "else". Python raise all errors, if you want to "skip" one, except AttributeError: pass is the way to go. It's not pointless, you are explicitly saying you want to let this error pass (or apply another process, or whatever). EDIT: And, no there is not while not AttributeError Commented Dec 29, 2018 at 17:08
  • If there’s no exception raised, would it repeat anything? Commented Dec 29, 2018 at 17:10
  • @DavisHerring No, it would run the block until the end, and it would continue on regardless if there was an error or not. If there was an error, it would end early and just continue on. Commented Dec 29, 2018 at 17:11
  • Is this an infinite loop? What is the stopping condition? Does it break when an exception is encountered? Commented Dec 29, 2018 at 17:17
  • This is not how Exceptions should be utilised - please use the standard try/except methodology. Commented Dec 29, 2018 at 17:18

4 Answers 4

13

Can you try something like:

while True:
 try:
 print("ok")
 print(str.translate)
 print(str.foo)
 except:
 break
print('done')
answered Dec 29, 2018 at 17:22
Sign up to request clarification or add additional context in comments.

Comments

5

The following code will loop until it encounters an error.

while True:
 try:
 print("ok")
 print(str.translate)
 print(str.foo)
 except AttributeError:
 print("oops, found an error")
 break
 print("done")
answered Dec 29, 2018 at 17:23

Comments

1

You can use suppress() as an alternative to try/except/pass available for python 3.4+

from contextlib import suppress
while True:
 with suppress(AttributeError):
 print("ok")
 print(str.translate)
 print(str.foo)
 break
print('done')
answered Sep 10, 2019 at 16:59

Comments

1

You can nest while loop:

try:
 while True:
 print("ok")
 print(str.translate)
 print(str.foo)
except AttributeError:
 print('done')

In this case you don't need to call break explicitly.

answered Apr 2, 2020 at 16:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.