One of the seven sacraments of the Catholic Faith is the anointing of the sick:
“The Apostles anointed the sick with oil and cured them.” (Mark 6:13).
“The presbyters are called to anoint the sick with oil and pray over them. Their sins are forgiven” (James 5:14).
Writing in the 300s, Saint John Chrysostom testified to the sacrament’s power:
“For not only at the time of regeneration, but afterwards also, they have authority to forgive sins. ‘Is any sick among you?’ it is said, ‘let him call for the elders of the Church and let them pray over him, anointing him with oil in the name of the Lord. And the prayer of faith shall save the sick, and the Lord will raise him up: and if he have committed sins they shall be forgiven him.’”
John Chrysostom, On the Priesthood, 3:6 (A.D. 386)
Notice how Saint Chrysostom confirms the words of Saint James that this sacrament “forgives sins”. This is why it administered in illness and before death.