It t is the SAME situation since joseph, a JUST MAN , assumed Mary was involved in sexual sin. Per scripture, joseph acted accordingly to not "out her" in order to put her to shame being the "just" man he was. God later informed him otherwise and had a plan accordingly for them both
If you are saved. this two scriptural verses are totally straightforward. If not, I expect this could be confusing. We know exactly how just men, including our Lord Jesus, handle these circumstances. And it has nothing to do with punishment, condemnation or shaming of anyone.