I've seen a few TV episodes that involve a trial ("Bart the Murderer" from The Simpsons, "Lois Kills Stewie" from Family Guy) where the judge announces a jail sentence and is about to hit his gavel but is interrupted by somebody who's able to clear the name of the accused, saving them from being arrested. These scenes imply that without the interruption, or perhaps if they showed up a few seconds too late, the accused would have gone to jail even when it's proven they are innocent.
So in real life, if something like that happened, would the accused still go to jail? Both examples I mentioned were times when characters were accused of murdering somebody, who ended up not being murdered. I don't know if there's any real-life examples of such things (and if there are real-life examples of somebody being sentenced to jail for murder and then it's later discovered that the accused is not only innocent but the one believed to be murdered is alive and well after all, I doubt there's any real-life examples of them showing up in the courtroom during the trial).
This just put a thought in my head... If somebody is given a jail sentence and then it's later proven that they didn't commit the crime after all, does the accused get some kind of award or benefits? The wrongfully accused should be given some kind of benefit for being wrongfully accused.