On-block will work with sub-flows, you should re-direct any errors that it catches to another subflow that will do what the guys above have suggested, another method incase you need it is to use a "Get window" with foreground option and "Close window" activities together in a while loop which checks for "Close window" to throw an error
(simply have an on-error for "Close window" that sets a variable for the while loop to check),
Once it does detect one, it ends as it has reached the desktop and cannot close anything further. This won't work if the windows are minimized though.