a part that executes only at the end | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

a part that executes only at the end

I have a part of code that i want it to be executed only when i STOP the program. For example closing a connection to database when my program is stopped manually by me. does finally statement do that for me? Like this: try: while true: do_something_with_database() finally: connection_to_database.close() considering that the while loop is infinite until i stop program manually. Any other idea? Tnx

2nd Aug 2018, 2:15 PM
Aref moloughi
Aref moloughi - avatar
1 Answer
+ 1
Well, the program will do the finally code for you, but it also will get a KeyboarInterrupt error, besides, there might be some platform-specific and user-specific issues. By all means I believe this is a bad idea to interrupt programm execution manually - you'd better do some programmable user iteraction or some kind of watchdog system to interrupt programm by event.
2nd Aug 2018, 2:40 PM
strawdog
strawdog - avatar