To recap the story from Matthew’s Gospel:
When Mary and Joseph were betrothed, but had not yet lived together as man and wife, Joseph learned that she was with child. (At this time, according to the Jewish marriage customs, a couple was betrothed for several months before moving in together and having marital relations.) Naturally, he assumed that she had been unfaithful to him with some other man. The evangelist Matthew describes what happened next: “Joseph her husband, since he was a righteous man, yet unwilling to expose her to shame, decided to divorce her quietly. Such was his intention when, behold, the angel of the Lord appeared to him in a dream and said, ‘Joseph, son of David, do not be afraid to take Mary your wife into your home. For it is through the holy Spirit that this child has been conceived in her.’” (Matthew 1:19-20)
Notice that Matthew does not specify whether anyone else knew of Joseph’s decision to get a divorce from Mary. It’s possible that Joseph hadn’t yet shared his plan of action with Mary. Even if he did tell her, we have no record of how she did react. Theoretically, it’s possible that she could have been angry, though that seems unlikely; divorce is better than being killed, which was the penalty Mary would have suffered under the Mosaic law for being pregnant out of wedlock (Leviticus 20:10). In that sense, Joseph was being extremely compassionate.
In the end, we don’t know the whole story. What we do know is that Joseph was a good guy who tried, as much as he could, to protect Mary. With a little heavenly intervention, everything worked out just fine in the end.