canada
The existence of a defence is relevant, to various degrees, at every stage of the arrest and prosecution.
As an example, the self-defence provision in Canada starts with: "A person is not guilty of an offence if..."
If the circumstances meet the criteria for self-defence, then the person did not commit an offence.
At the arrest stage, absent a warrant, an officer needs to believe on reasonable and probable grounds, that the person committed or is about to commit an indictable offence or whom he or she finds is commiting an offence. That must take into account information that would inform whether a defence would be available. However, at this stage, the officer is not expected to have conducted a full or conclusive analysis. "Reasonable and probable grounds" is much lower than the "beyond a reasonable doubt" standard by which the defence would have to be disproved at trial by the Crown. It is even lower than the civil standard of balance of probabilities.
As the Court of Appeal for British Columbia said:
This standard does not require an officer to satisfy him or herself that there is evidence of proof beyond a reasonable doubt or even a prima facie case. All that the officer must have is an objectively reasonable basis for believing the suspect is presently in possession of marihuana, without necessarily ruling out potentially innocent inferences, defences or lawful excuses. ... There is no merit to the appellant’s argument that the officer was required to rule out potential innocent explanations before it could be said he had reasonable grounds for the arrest.
There are many permissible arrests in circumstances where there is ultimately a successful defence.
Similarly, while the decision to prosecute in Canada is nearly completely free from judicial scrutiny, every jurisdiction follows an internal charging policy that considers whether there would be a "substantial likelihood of conviction" (or similar threshold). This includes consideration of defences. In British Columbia, for example, the charge assessment guidelines direct prosecutors to consider any defences, or other legal or constitutional impediments to prosecution, that remove any substantial likelihood of conviction. If there is a defence that appears strong enough at this stage to remove a substantial likelihood of conviction, prosecutors have been directed to not bring a charge. And if, after bringing a charge, facts emerge after prosecution that change the initial assessment, prosecutors have been directed to drop the charge.