LONDON—The British economy unexpectedly contracted in the second quarter as uncertainty over the country’s planned departure from the European Union on Oct. 31 took its toll on business confidence.

The uncertainty has since intensified. New Prime Minister Boris Johnson has pledged to leave the bloc without a divorce deal if EU leaders refuse to negotiate a new accord. The EU has said the deal reached with Mr. Johnson’s predecessor, but rejected by U.K. lawmakers three times, isn’t up for negotiation. With a majority of lawmakers...