WASHINGTON — Defense Secretary Chuck Hagel delivered an aggressive defense Wednesday of the secret prisoner exchange of five Taliban detainees for a U.S. soldier, telling Congress that the risks were too great and the situation too uncertain for the administration to tell lawmakers about the plan.

In a nearly five-hour Capitol Hill hearing that was at times contentious, House members accused Hagel and the White House of not trusting them enough to follow the law and fill them in on the decision to exchange Sgt. Bowe Bergdahl for five detainees at the U.S. facility at Guantanamo Bay, Cuba.

An equally combative Hagel said the deal provided “the best possibility that we had to get him out, and we were concerned we might lose it.” He said officials discussed the law requiring that Congress get a 30-day notice of detainee exchanges but said the Justice Department told them that in such extreme circumstances President Obama had the constitutional authority to forgo the notice.

The Justice Department declined to comment on advice it gave the White House.

The first Obama administration official to testify about the prisoner swap, Hagel acknowledged up front that the matter could have been handled better. But he offered no apology for keeping Congress in the dark, insisting that Qatari officials negotiating the swap between the U.S. and the Taliban made it clear that “time was not on our side.”

U.S. officials said last week that intelligence suggested that the captors might have killed Bergdahl if word of the swap leaked out.

Calling the operation an “extraordinary situation,” Hagel said there were concerns about Bergdahl’s health and safety, the last-minute arrangements over where to pick up the soldier and persistent fears the Taliban may have been negotiating in bad faith. He said the deal only came together in the final 96 hours.

Republicans and Democrats questioned the wisdom of releasing the five Taliban members, saying they could return to the battlefield.

Administration officials have told Congress that four of the five Taliban officials will likely rejoin the fight.