Section 12E. Whoever discharges a firearm as defined in section one hundred and twenty-one of chapter one hundred and forty, a rifle or shotgun within five hundred feet of a dwelling or other building in use, except with the consent of the owner or legal occupant thereof, shall be punished by a fine of not less than fifty nor more than one hundred dollars or by imprisonment in a jail or house of correction for not more than three months, or both. The provisions of this section shall not apply to (a) the lawful defense of life and property; (b) any law enforcement officer acting in the discharge of his duties; (c) persons using underground or indoor target or test ranges with the consent of the owner or legal occupant thereof; (d) persons using outdoor skeet, trap, target or test ranges with the consent of the owner or legal occupant of the land on which the range is established; (e) persons using shooting galleries, licensed and defined under the provisions of section fifty-six A of chapter one hundred and forty; and (f) the discharge of blank cartridges for theatrical, athletic, ceremonial, firing squad, or other purposes in accordance with section thirty-nine of chapter one hundred and forty-eight.