A federal appeals court threw out a lawsuit accusing President Donald Trump of illegally profiting from his Washington hotel, finding the attorneys general of Maryland and Washington hadn’t shown their constituents were harmed by Trump’s actions.

The ruling by a Richmond, Virginia-based appeals court hands a victory to the president, who can now avoid having the two Democratic attorneys general delving into his finances to determine if he violated the U.S. Constitution’s emoluments clauses.

“Unanimous decision in my favor from...