NAIROBI, Kenya — Human rights groups expressed outrage on Thursday over a Kenyan court’s decision to uphold mandatory anal examinations of men who are suspected of being gay.

In Kenya, a colonial-era law prohibiting “carnal knowledge of any person against the order of nature” is widely understood to prohibit anal sex or sex between men. On Thursday, a court in Mombasa denied a petition to overturn the government’s practice of subjecting men to forced anal exams.

While human rights groups criticized the exams as abusive and medically worthless, government officials argued that they were a useful way to tell if a man was gay.

“This ruling is a devastating precedent that has now heightened the risk and fear of similar anal testing on many lesbian, gay, bisexual, transgender, intersex and queer persons in Kenya,” said Eric Gitari, executive director of the National Gay and Lesbian Human Rights Commission, a Kenyan advocacy group.