In rabbinic literature Joseph is described as Yosef Hatzaddik, Joseph the Righteous, a high accolade which we are taught that he earned because of the way that he resisted the sexual advances of Potiphar's wife.
One of the questions that comes to mind when reading the episode is why was he so careless in leaving his shirt in his mistress's hand when he fled from her? Had he fought with her so that he could have taken it with him he would have saved himself much grief and would never have ended up being thrown in jail.
One answer to this question is to point out that this is a fine example of divine providence; he had to leave his shirt behind so that he could be thrown into jail. For had he not been thrown into jail he wouldn't have interpreted the dreams of the butler and the baker, and without interpreting their dreams he would never have been called to interpret Pharaoh's dreams and all the rest would never have become history.
But there is another reason as well as to why he left his shirt behind, because he was actually more frightened by the thought of doing the wrong thing and sinning than by the thought of getting caught.
Too often, people's behaviour is dictated by how they assess the chances of being caught out rather than by an assessment of the morality of what they are doing. Joseph was quite different in that regard. He was willing to take the risk of facing trumped-up charges and their consequences rather than run the risk of doing something that was unconscionable.
And more than likely that is why he is described as being righteous. Not just because he did the right thing, but because his fear of sin exceeded his fear of being caught.