If an unmarried young man has cancer and must undergo radiation therapy and other treatments that may permanently affect his virility, it is a mitzva for him to provide sperm prior to the treatment, so that it can be frozen and then used to impregnate his eventual wife. Even if he is too young to be contemplating marriage, once he has reached the age of thirteen he is obligated in mitzvot. To fulfill the mitzva of procreation, there is a mitzva for him to produce semen that can later be used to have children. This is not wasting seed, as it is for the sake of procreation. Even if the treatments might not render him sterile, in which case it will have been unnecessary for him to provide sperm, he still has a mitzva to do so in order to guarantee his future ability to have children and fulfill the mitzva. If he can provide sperm without touching himself, that is preferable; if not, he may stimulate himself manually.
If a husband or wife is HIV-positive, then every time they have sexual relations, the spouse is at risk of contracting the virus. The only way to avoid this is to use a condom, which protects the healthy spouse. Some poskim rule that the couple may not have sexual relations, since regular condom use is considered wasting seed, and the couple must therefore divorce (Minḥat Shlomo 3:103:16). Others maintain that the prohibition of condom use is for its usual purpose, namely, contraception. However, if it is being used to protect the life of the husband or wife, they may use a condom, so they can at least fulfill the mitzva of ona (Aḥiezer 3:24:5; Igrot Moshe, EH 1:63; Tzitz Eliezer 9:51:2). Halakha follows the latter opinion.