0

I am running a python project in pycharm. In the code we have a main "try-catch-finally" block e.g.

try:
   # Some stuff like opening files and video streams
except SomePossibleExceptions:
   # Handle possible exception
finally:
   # Save, close and tidy up unfinished files / videos / output streams

If I run the program in the terminal it will reach the finally block when I press our quit button or "ctrl-c" and perform the post processing required. However, after pressing "stop" when using the run tool in PyCharm it just quits and does not reach the finally block.

The obvious answer is to just run in the terminal but is there a way to get PyCharm to run the finally block after pressing "stop" in the run tool?

  • 1
    What do you mean for "STOP"? Which key? Note: Control-C is sent as a signal to the program. Other job control command may not read your program, but just the parent process. It is common to kill a program, without ever getting any chance for him to execute code. This is a very useful feature. – Giacomo Catenazzi Jul 23 '20 at 12:32

1 Answers1

1

No, using the red stop button terminates the process immediately.

I think you alluded to this, but the only workaround I have found is to edit the run configuration:

run > edit configurations > tick the box for 'Emulate terminal in output console' or 'Run with Python Console'.

With either of those selections ticked, the run window will accept ctrl-C inputs which will allow your finally block to execute.

JCollinski
  • 54
  • 1
  • 6