A federal judge threw out former Pennsylvania State University president Graham B. Spanier’s conviction on child endangerment charges Tuesday, granting him a last-minute reprieve just hours before he was due to surrender to jail.

U.S. Magistrate Judge Karoline Mehalchick agreed that the ex-university executive had been improperly tried under a version of the law passed six years after his alleged mishandling of a 2001 complaint involving former assistant football coach Jerry Sandusky.

The Scranton-based judge gave state prosecutors three months to retry the case under the statute in place at the time of Spanier’s alleged crime. That version of the law did not specifically require supervisors who oversaw staff responsible for the welfare of children to report suspected abuse — something legislators added in 2007.

Prosecuting him under the newer revision “unreasonably expanded the scope of the pre-amendment child endangerment statute in such a way that it would have been unforeseeable to Spanier in 2001 that his conduct could result in criminal culpability,” Mehalchick wrote.

Spanier, 70, had been ordered to report to jail Wednesday morning to begin serving a minimum two-month jail sentence followed by two months’ house arrest. He and his lawyers declined to comment Tuesday night.

Still, the judge’s unusual, eleventh-hour reprieve delivered them a significant victory that is certain to reopen a debate that has roiled Penn State’s campus since Sandusky’s arrest nearly eight years ago.

Though prosecutors have argued that Spanier and other top school administrators chose to protect the school and their own reputations over their duty to protect children from a serial sex predator, several ardent Penn State supporters maintain that investigators, the media, and the university’s own trustees rushed to judgment in an effort to appear responsive to Sandusky’s crimes.

“For the first time in 7½ years, I feel good,” said Anthony Lubrano, a former alumni-elected university trustee who felt Spanier, his senior managers, and former head football coach Joe Paterno were wrongly vilified. “This decision … at least temporarily restores my faith in the judicial system, and I think in this case that justice has finally been served.”

What happens next for Spanier — who remains a tenured member of Penn State’s faculty and was once ranked among the nation’s most respected and longest-serving university leaders — remains uncertain.

The Pennsylvania Attorney General’s Office could appeal Mehalchick’s ruling to the U.S. Court of Appeals for the Third Circuit in Philadelphia or decide to retry Spanier as the judge ordered.

Joe Grace, an office spokesperson, said Tuesday that prosecutors were reviewing the judge’s opinion carefully.

Spanier and two top aides — former Penn State athletic director Tim Curley and ex-university vice president Gary Schultz — were convicted in 2017 for failing to adequately respond to a complaint from then-graduate student Mike McQueary, who reported in 2001 that he saw Sandusky abusing a boy in a locker-room shower.

The men agreed to notify the Second Mile, a charity Sandusky founded for at-risk youth and the source of many of his victims. But they did not report McQueary’s complaint to authorities.

Spanier signed off on that plan in an email, warning that “the only downside for us is if the message isn’t ‘heard’ and acted upon, and we then become vulnerable for not having reported it.”

Since then, he has said at his sentencing he regretted not intervening more forcefully. But in arguments before Mehalchick, a Penn State graduate, his lawyers maintained his actions did not violate the state’s child endangerment laws as they were written at the time.

In 2001, the law required only “a parent, guardian, or other person supervising the welfare of children” to report suspected abuse.

The statute was revised in 2007 to also include “a person that employs or supervises such a person” — an amendment made in response to grand jury investigations that resulted in no charges against leaders of the Roman Catholic Archdiocese of Philadelphia for their mishandling of clergy sex-abuse cases.

Prosecutors argued that the 2007 revision did not actually change the law but merely added specific references to a group that was inherently included in the earlier version.

But Mehalchick disagreed. She concluded that as a university president with no direct contact with any of Sandusky’s victims, Spanier could not have reasonably foreseen that he was included in the mandatory reporting categories laid out in the law as it stood in 2001.

Trying him under the later version of the law was a violation of his constitutional rights, she said.

Curley and Schultz both pleaded guilty to misdemeanor child endangerment charges on the eve of their trial and testified as government witnesses in Spanier’s case. Both have since served jail terms and waived their appellate rights as part of their plea agreement with the government.

Sandusky is serving a 30- to 60-year sentence at a state prison in Somerset County.

Since 2011, the university has paid out hundreds of millions of dollars in settlements, legal bills, fines, public relations, and other costs stemming from the scandal.

A university spokesperson declined to comment Tuesday.

Staff writer Susan Snyder contributed to this article.