This question already has an answer here:
- Yehareg v'al ya'avor – what about after? 2 answers
For the three sins of murder, idol worship and immorality, one is to give their life over rather than transgress. If one was in such a situation and transgressed, is it a mitzvah for them to kill themselves afterwards (disregarding local laws etc.)?