WASHINGTON — Former Defense Secretary Donald H. Rumsfeld cannot be tried on allegations of torture in overseas military prisons, a federal judge said Tuesday.

U.S. District Judge Thomas F. Hogan threw out a lawsuit brought on behalf of nine former prisoners in Iraq and Afghanistan. He said Rumsfeld cannot be held personally responsible for actions taken in connection with his government job.

The lawsuit contends the prisoners were beaten, suspended upside down from the ceiling by chains, urinated on, shocked, sexually humiliated, burned, locked inside boxes and subjected to mock executions.

Lawyers for the American Civil Liberties Union and Human Rights First had argued that Rumsfeld and top military officials disregarded warnings about the abuse and authorized the use of illegal interrogation tactics that violated the constitutional and human rights of prisoners.

“Despite the horrifying torture allegations,” Hogan wrote, he could find no case law supporting the lawsuit, which he previously had described as unprecedented.

Government officials are normally immune from such lawsuits, and foreigners held overseas are not normally afforded U.S. constitutional rights.

Allowing the case to go forward, Hogan said in December, might subject government officials to all sorts of political lawsuits. Even Osama bin Laden could sue, Hogan said, claiming two American presidents threatened to have him murdered.

Had the Rumsfeld lawsuit been allowed to go forward, attorneys for the ACLU might have been able to force the Pentagon to disclose what officials knew about abuses at prisons such as Abu Ghraib prison in Iraq and what was done to stop it.