"Now the birth of Jesus Christ was as follows: when His mother Mary had been betrothed to Joseph, before they came together she was found to be with child by the Holy Spirit. And Joseph her husband, being a righteous man and not wanting to disgrace her, planned to send her away secretly."
Matthew 1:18-19
Joseph and Mary's betrothal was a binding agreement for marriage. Although the betrothed couple did not yet live together as husband and wife, the arrangement required a divorce to break it. As Mary and Joseph awaited their coming marriage, Mary became pregnant. Upon learning of her condition, Joseph planned to break the engagement -- to divorce Mary. According to Jewish law, Joseph could have taken Mary to court to divorce her for adultery, which could have resulted in Mary being taken outside the city gates and stoned to death. At the very least, the public divorce would have ruined her reputation. But Joseph reacted honorably. He did not respond to his perceived betrayal with thoughts of revenge. he thought to divorce her quietly.
"The shock to Joseph of the sudden discovery, crashing in on him after he was bound to Mary, and in what would else have been the sweet interval of love and longing ‘before they came together,’ is delicately and unconsciously brought out in verse 18. ‘She was found’—how the remembrance of the sudden disclosure, blinding and startling as a lightning flash, lives in that word! And how the agony of perplexity as to the right thing to do in such a cruel dilemma is hinted at in the two clauses that pull in opposite directions! As a ‘just man’ and ‘her husband,’ Joseph owed it to righteousness and to himself not to ignore his betrothed’s condition; but as her lover and her husband, how could he put her, who was still so dear to him, to public shame, some of which would cloud his own name? To ‘put her away’ was the only course possible, though it racked his soul, and to do it ‘privily’ was the last gift that his wounded love could give her." -- Alexander McLaren