The exception clause might have been included in Matthew to address an issue particularly affecting the Jews of his day, namely, the discovery of adultery before the consummation of a marriage. I’m guessing that marriages today are usually consummated the same day the marriage vows are exchanged and so this would not be much of a concern. However, back then, I understand, Jewish couples typically waited a whole year after they exchanged marriage vows before they started living together and consummated their marriage. Matthew alone tells us that it was during this extended period between the exchange of marriage vows and the consummation of their marriage that St Joseph discovered that his wife, the Virgin Mary, was pregnant and he decided to divorce her. Matthew’s portrayal of Joseph as a righteous man precludes Joseph from doing anything against the teachings of Jesus. Thus, there was a need for Matthew to include the exception clause to reconcile Joseph’s decision to divorce his wife and end their unconsummated marriage with Jesus’ teaching against divorce of consummated marriages.
The current Code of Canon Law has a similar provision, permitting, for a just reason and with the pope’s approval, the dissolution of a valid marriage between Catholics, if their marriage has not yet been consummated:
Canon 1142. A non-consummated marriage between baptised persons or between a baptised party and an unbaptised party can be dissolved by the Roman Pontiff for a just reason, at the request of both parties or of either party, even if the other is unwilling.