Between 1940 and 1946, under the Treachery Act, The British prosecuted and executed 19 spies who were working for the Germans. They captured more spies, but if their capture was not known to the public then they did not prosecute them but instead turned them to work against the Germans.
The Treachery Act was brought in quickly in 1940, largely at Churchill’s urging, because the authorities doubted whether under current legislation they could prosecute a foreign spy for treason.
And the reason for wanting to be able to prosecute for treason was that the punishment for the crime of treason was death. And the threat of death was thought to be a strong motivator to convince a captured spy to change allegiance.