He planned to divorce her quietly. What a good guy. He’s been told his fiancé, Mary, is pregnant. But he’s not the father. But the law provides him with an out. But rather than publicly humiliating Mary, Joseph decides to break it off quietly.
Based on my various readings, most men, at the time, would have called the engagement off. The only question was how best to do it.
His plan was sound. He felt humiliated and didn’t quite understand the miraculous nature of the situation. I take it Joseph wasn’t fully aware of his lineage or the significant part he was to play in being the second last in the line that began with Abraham (Matthew 1:17)