Every person is presumed to know the laws of the land in which they dwell.
As judges like to say, ignorance of the law is no defense to criminal charges. There are exceptions, but the overwhelming majority of crimes don't require that the defendant know that their conduct is illegal. Allowing mistake of law as a defense for every crime would encourage people to remain ignorant of the law.
A person is guilty of a crime based on their knowing actions, not their knowledge of the particular law. For example, a person who knowingly shoots a gun into the air at a festival might not realize they committed a crime. But that doesn't mean an officer can't charge the person with reckless endangerment for creating a substantial risk of serious bodily harm to another.
As generally applied, this rule isn't controversial. For instance, it's hard to imagine someone sincerely arguing that they didn't know that theft or driving under the influence of alcohol was illegal.
Mistake of law defenses are rare, but in some cases, it could negate an element of a crime. Take this case as an example.
Example. A bail collection agent (bounty hunter) attempted to apprehend a fugitive for jumping bail. State law gave the agent the right to enter and search the fugitive's listed residence. When arriving at the residence, the occupants refused to let the agent enter. The agent asserted his "legal right to enter" and searched the home. He was later charged with criminal trespass. Criminal trespass requires the prosecution to prove the defendant entered another's property knowing he was not licensed or privileged to do so. Because the defendant believed he had a legal right to enter, he was entitled to a mistake-of-law jury instruction. (State v. Wickleff, 875 A.2d 1009 (N.J. App. 2005).)
Another very narrow exception exists when a defendant believes their conduct is lawful based on reasonable reliance of an official statement of law or grant of permission by an administrative authority. This doesn't include reliance on attorney advice.
Example. In a Texas case, a defendant was convicted of illegally voting. The law makes it a crime to vote in an election in which the person knows they are not legally eligible to vote. The defendant's claimed he reasonably believed he was eligible based on his reliance of election law authorities' interpretation of the law. The appellate court sent the case back to the trial court to have a jury decide this mistake-of-law issue. (Jenkins v. Texas, 468 S.W.3d 656 (Ct. App. 2015).)
People charged with crimes often argue that they were mistaken not as to the law, but as to the facts. Factual mistake is more likely to provide a legitimate defense than legal mistake. But not all crimes lend themselves to a mistake-of-fact defense.
For example, people charged with statutory rape commonly assert that they didn't know their partner wasn't old enough to consent. In this situation, the defendant isn't arguing mistake of law (that they didn't know having sex with a minor was illegal), but rather, mistake of fact (that they didn't know how old their sexual partner was). Mistake of fact is often a viable defense to criminal charges, but in most states, it's not when statutory rape is involved.
If you're facing criminal charges, talk with a criminal defense attorney. A lawyer can help you understand the laws involved, what's at stake, and whether any defenses apply based on the circumstances of your case.