Quantcast
Viewing all articles
Browse latest Browse all 5114

General discussion • Re: Python Script keeps Exporting after Finally Statement

I've no idea if this is the problem but:

In several places you have recording.some_method() but I couldn't see where you first define or assign to recording.
[edit] My mistake. It's a parameter to the function.[/edit]

In a lot of places you're also completely surpressing all exceptions. That really won't help debug things and will stop a higher level exception handler from running.

So, if you hit ctrl-C while in, say, write_file() the exception handler in main() will never see it.

[edit]If you want the ctrl-C to propogate upwards use except Exception: not except:.[/edit]
I'm only using ctrl-C to test it. The except clause is to catch any error. The code is supposed to work in the finally statement right? Because if a script externally closes it while I'm recording, that's NOT a ctrl-C close.

Kinda.

The problem with your code is that several of your functions trap all exceptions and cause them to be ignored and you have an infinite loop in your main() function.

The finally code gets executed after the exception handler has run and once your code exits the try: ... except:blocks whether through an exception or not.

If an exception occurs in one of your functions with tr:y ... except: pass the exception handler in main() will never see it. Instead the function will return execution to the main thread but with the things it changes in an undefined state. Which may cause a further exception.

Becasue your loop is infinite, the only way your code will ever reach the finally block is if you have an exception within main().

Try running this (NB: untested):

Code:

import timetry:    while True:        try:            print('hit ctrl-c')            time.sleep(15)        except:            print('you hit ctrl-c')except:    print('you're very unlikely to ever see this')finally:    print('or this')

Statistics: Posted by thagrol — Sun Oct 06, 2024 11:04 pm



Viewing all articles
Browse latest Browse all 5114

Trending Articles