2

If some error comes then it goes to except statement after the try one, where the program ends. My question is, Is it possible, that if an error comes then without ending the program it runs the try statement continuously?

For example :

try:
    some error caught
except:
    go to try statement again

And this runs continuously in a chain?

  • Sounds like you might want recursion. Look at [this SO post](https://stackoverflow.com/questions/39059566/python-try-except-else-with-recursion). – Jack Moody Apr 17 '20 at 01:39
  • You can include a try-except in a while- or for-loop. – Michael Butscher Apr 17 '20 at 01:39
  • Yeah i did put in a while loop, but when an error comes, it ends the program : ( –  Apr 17 '20 at 01:41
  • 1
    please try to provide the smallest working example, that reproduces the error. If this is not possible, then show the error message, that you get. – gelonida Apr 17 '20 at 01:55
  • I just answered my own question, and worked perfectly in my case. Thanks for your help : ) –  Apr 17 '20 at 01:56

1 Answers1

0

Just create a loop, that breaks if no exception occurs

while True:
   try:
       some_code_that_might_fail
    except Exception:  # catch all potential errors 
        continue  # if exception occured continue the loop
    break  # if no exception occured break out of the loop

Pls try out following example:

while True:
    try:
        num = int(input("please enter a number"))
        print("The number is ", num)
        rec = 1 / num
    except Exception:
        print("either you entered no number or 0 (or some other error occured)")
        continue  # if any exception occured continue the loop
    break  # if no exception occured break out of the loop

print("1 / %f = %f" % (num, rec))

As Bruno mentioned. In general (and this is what I do in my own code) it is not suggested to catch all exceptions.

You should catch only known exceptions explicitly

Addendum 2020-04-17

Reading your answer I think your question is a little misleading. What is perhaps your problem is, that you have a function, that you would like to run forever. However sometimes the function terminates (due to an error) without raising an exception.

IF this is the case then just write:

while True:
   afunc()
   print("function terminated. I will restart it")

but note, that your program will never terminate.

or if the function sometimes raises an exception and sometimes doesn't but just terminates and you'd like to call the function whenever it failed or terminated, then do.

while True:
   try:
      afunc()
      print("function terminated without exception")

   except Exception:
      pass
      print("function encountered an exception")
   print("will restart")

If you want, that function can terminate and you have the means to find out whether it was an error or not, then you could do something like:

while True:
   try:
      afunc()
      if i_know_that_func_terminated_correctly():
          print("function terminated correctly")
          break
      print("function terminated without an error")

   except Exception:
      pass
      print("function terminated with an exception")
   print("restarting")

I added print statements for debugging / visualizing. Just remove them or comment them if not needed. (That's also why I left the pass statement in the except clause)

gelonida
  • 4,811
  • 2
  • 17
  • 37
  • I just tried your code and if error comes It again ends the program : ( –  Apr 17 '20 at 01:44
  • Perhaps your code does not raise an exception, but calls calls `os._exit()` on error This cannot be caught with `try:` / `except:` I will show you in an example, that above code works for exceptions. You might want to post the error message that you get. there might be tricks with monkeypatching or mocks but we need more info about your exact problem – gelonida Apr 17 '20 at 01:52
  • Please do NOT post example code with bare except clauses - those are PURE EVIL. – bruno desthuilliers Apr 17 '20 at 05:57
  • In the given context I assume to be pure evil. The question asks explicitly to repeat on all errors. (to catch all errors) I can add `Exception` though. However I added a comment to my answer – gelonida Apr 17 '20 at 10:20
  • also changed the code to make it more obvious, that ALL exceptions are caught – gelonida Apr 17 '20 at 10:26
  • funny: got an undownvote, followed by a downvote within the same minute. Unfortunately no comment. I'd like to improve the answer if possible, but commentless voting doesn't help to do this – gelonida Apr 20 '20 at 10:54