WASHINGTON — A federal judge handed a partial victory to the former CEO of American International Group, who claimed the government’s bailout of the insurance giant in the heat of the financial crisis was unfairly punitive.

Judge Thomas Wheeler ruled Monday to validate the allegation made in the lawsuit by former AIG Chairman and CEO Maurice Greenberg. But the judge rejected Greenberg’s demand for some $40 billion in damages for himself and other AIG shareholders from the government.

The suit alleged that the $85 billion bailout of the teetering insurance giant in September 2008 violated the Constitution’s Fifth Amendment by taking control of AIG without “just compensation.”

The unusual case raised the issue of limits on the government’s power in responding to financial catastrophe. It brought the rare spectacle of back-to-back courtroom testimony in a trial last fall by three former leaders of the government’s bailout — then-Federal Reserve Chairman Ben Bernanke, Treasury Secretary Henry Paulson and New York Fed President Timothy Geithner.

The eight-week trial, without a jury, took place in the U.S. Court of Federal Claims. Greenberg’s lawsuit, filed in November 2011, had been deemed a long shot by many legal experts.

Wheeler called the government’s conduct in its taking control of 80 percent of AIG’s stock an “illegal exaction” and criticized its “unduly harsh treatment of AIG in comparison to other institutions.”

The Fed “possessed the authority in a time of crisis to make emergency loans to distressed entities such as AIG, but they did not have the legal right to become the owner of AIG,” Wheeler wrote. “There is no law permitting the Federal Reserve to take over a company and run its business in the commercial world (in exchange) for a loan.”

Officials asserted during the trial that the government imposed losses on shareholders of bailed-out companies that were in proportion to the bad decisions made by their managers. That would appear to explain the big equity stake the government took in AIG and the interest rate on the taxpayer-backed loan, which was set at about 12 percent annually.

That was much higher than what other big financial companies paid in the bailout. For example, there was a 3.5 percent rate on the government’s loan to Citigroup Inc., which received a $45 billion bailout.

At the same time, Wheeler said in his ruling it is undeniable that AIG would have collapsed and filed for bankruptcy if the government hadn’t stepped in with aid. That would have pushed to zero the value of the shareholders’ stock. By making the loan to AIG, the government “significantly enhanced” the value of the stock, he said.

Therefore, while the takeover of 80 percent of the stock and the running of AIG’s business violated the law, “the government did not cause any economic loss to AIG’s shareholders,” Wheeler wrote.

The government may appeal Wheeler’s decision, which would be weighed by a federal appeals court. If it stands, it could have an impact on future government actions toward failing financial companies in times of crisis.

Justice Department spokeswoman Nicole Navas said the agency is reviewing the decision. Greenberg’s representatives didn’t have an immediate comment.