CCC 2154 Following St. Paul,1 the tradition of the Church has understood Jesus’ words as not excluding oaths made for grave and right reasons (for example, in court). “An oath, that is the invocation of the divine name as a witness to truth, cannot be taken unless in truth, in judgment, and in justice.”2

1 Cf. 2 Cor 1:23; Gal 1:20.
2 CIC, can. 1199 # 1.