Where an action provided for in sections 616.17 and 616.19 is against several defendants, some of whom are residents and others nonresidents of the county, and the action is dismissed as to the residents, or judgment is rendered in their favor, or there is a failure to obtain judgment against such residents, such nonresidents may, upon motion, have said cause dismissed, with reasonable compensation for trouble and expense in attending at the wrong county, unless they, having appeared to the action, fail to object before judgment is rendered against them.