A Montgomery County police officer avoided injury Sunday when a man he approached about roaming through a neighborhood and allegedly trying to break into sheds and at least one car fired a shotgun at him, Upper Gwynedd Township Police Chief David Duffy said.

“This is something every police officer worries about going on their shift ... ‚” Duffy told reporters at a briefing posted on Facebook. “Certainly it’s an extremely scary situation.”

The encounter in a backyard on the 1400 block of Cathys Lane occurred shortly after 1 p.m., nearly an hour after Upper Gwynedd officers first responded to a report of a suspicious man roaming a neighborhood at East Prospect Avenue and Hancock Road and encountered him near the Sanctuary United Methodist Church. When they attempted to speak to him there, he had turned away and crossed a large field, Duffy said.

Sgt. Ray Royds, a 21-year veteran with the department, accompanied by two other officers, had continued looking for the man, again encountering him in a backyard on Cathys Lane, Duffy said. When Royds again tried to talk to him, the man yelled obscenities and fired a shotgun toward him from about 30 feet away, Duffy said.

“Somehow that shot missed him,” the police chief said, saying Royds then opted for restraint and “a survival tactic” over firing back.

“Sgt. Royds had two choices: to try to unholster and fire back or try to get to the nearest oak tree,” Duffy said. “He chose the oak tree and that worked out.”

A search of the area, joined by officers from nearby police departments, led to the swift apprehension of the man and recovery of a shotgun, a rifle, and live ammunition, Duffy said.

The man, described as being 35 and from Warrington Township, Bucks County, was taken to Abington-Lansdale Hospital-Jefferson Health for detox for methamphetamine use, Duffy said. He was expected to be charged with a variety of offenses Sunday night, the chief said.

Police were not sure whether the weapons were the man’s or items he might have found in homes throughout the region. The investigation continues.