0

I have this code which produces a lot of errors which causes it to break out of the loop. These errors are due to coding issue in this code or because of remote server(s) issue. Is there anyway to restart the code block no matter what the error was. What i was thinking was a while loop inside a while loop but for some reason it is still breaking out of iteration.

while True:
     while True:
         [CODE BLOCK]

Could you please advise how a code block can be forced to restart iteration no matter the error. If there is anyway to skip any error and just move on.

Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • use try - except – Wonka Aug 18 '23 at 10:23
  • Is there a error which just for error, like i have a long list of errors such as keyerror, json error, name error, value error, and some due to poorly written code in the try except block but there are error still coming up. Is it possible to just restart with 1 error name for all? Because these error are due to issues with how the server is sending out data, throttling, resetting and so on. @Wonka – Slartibartfast Aug 18 '23 at 10:26
  • yes you can control which exception you want to continue or stop code – Wonka Aug 18 '23 at 10:28
  • try - except Exception as e: what @Wonka said – Ashkan Goleh Pour Aug 18 '23 at 10:28
  • Yes, you can do a general try-except without specifying the type of error and still catch the error. For example: try: [code] except Exception as e: print(e) – MappingThePast Aug 18 '23 at 10:31

1 Answers1

0
while True:
    try:
        while True:
            try:
                # Your code block that might raise an error
                # ...
                
            except Exception as e:
                # Handle the error if needed
                print(f"An error occurred in inner loop: {e}")
            
            finally:
                # This block will always execute, allowing you to restart the inner loop
                print("Restarting inner loop...")
            
    except KeyboardInterrupt:
        # Allow the user to stop the process by pressing Ctrl+C
        print("Process stopped by user.")
        break
    
    except Exception as e:
        # Handle other exceptions that might occur in the outer loop
        print(f"An error occurred in outer loop: {e}")

the outer while True loop will keep restarting the inner loop regardless of whether an error occurred in the inner loop or not.

Remember that using a structure like this could potentially lead to an infinite loop if errors keep occurring repeatedly. It's important to have proper error handling and potentially add conditions to break out of the loops after a certain number of retries or after a specific condition is met.

  • There might be code that you want to put in a `finally` block, but there is no need for it to exist just in order to "restart" the loop. – slothrop Aug 18 '23 at 10:43
  • I understand your concern about not wanting to put unnecessary code in a finally block. If you only want to have a specific block of code that handles restarting the loop, you can structure it like this: while True: try: break except Exception as e: print(f"An error occurred: {e}") print("Restarting iteration...") This way, you achieve your goal of restarting the iteration without the need for a finally block that might contain other unrelated code. –  Aug 18 '23 at 10:56
  • 1
    Please be aware that ChatGPT-generated content is not allowed on Stack Overflow: https://stackoverflow.com/help/gpt-policy – slothrop Aug 18 '23 at 10:58
  • @slothrop how did you know this is chat gpt content? – Slartibartfast Aug 18 '23 at 13:20