0

My python script is (single threaded) running for many hours, however, after some time it suddenly stops. We used a try-except block to debug the error. However, this behaves very strange. We use:

try:
    while True:
        # do routine
        time.sleep(someSeconds)
except Exception as e:
    # exception!
finally:
    # finish

The while routine is executing normally, then the script (aka main thread) falls asleep, wakes up and executes the routine again. After hours the finally block gets executed, without the exception block being executed (first).

How can this be? As far as I know, when the while True loop stops for any reason, the except block should be executed (before finally)? How can it be that the error is not caught?

Note: there is no return/ break in the while loop

black
  • 1,151
  • 3
  • 18
  • 46

1 Answers1

0

Two options I can think of:

  • your “routine” exits the loop using return or break.

  • An exception is raised which does not derive from Exception (add an except: print(“other exception”) to test this)

Cortado
  • 26
  • 3
  • For the Sexton option: How do I get the name of this other exception? In the linked answer, the person says that ‘except Exception’ should catch every exception. And I thought any Exception inherits from *Exception*? – black Mar 08 '19 at 06:15
  • 1
    Not all exceptions derive from Exception. KeyboardInterrupt derives from BaseException for example. I found this example how to get the exception caught in the `except:` clause: `print("Unexpected error:", sys.exc_info()[0])` – Cortado Mar 08 '19 at 07:42