Law of Necessity

Compromising a law that is Bein Adam La'Makom (between man and God)

ר' שמעון בן מנסיא אומר (שמות לא, טז) ושמרו בני ישראל את השבת אמרה תורה חלל עליו שבת אחת כדי שישמור שבתות הרבה א"ר יהודה אמר שמואל אי הואי התם הוה אמינא דידי עדיפא מדידהו (ויקרא יח, ה) וחי בהם ולא שימות בהם
Rabbi Shimon ben Menasya said: It is stated: “And the children of Israel shall keep Shabbat, to observe Shabbat” (Exodus 31:16).The Torah said: Desecrate one Shabbat on his behalf so he will observe many Shabbatot. Rav Yehuda said that Shmuel said: If I would have been there among those Sages who debated this question, I would have said that my proof is preferable to theirs, as it states: “You shall keep My statutes and My ordinances, which a person shall do and live by them” (Leviticus 18:5), and not that he should die by them. In all circumstances, one must take care not to die as a result of fulfilling the mitzvot.

Compromising a law that is Bein Adam La'Chaveiro (between man and man)

רב הונא אמר גדישים דשעורים דישראל הוו דהוו מטמרי פלשתים בהו וקא מיבעיא ליה מהו להציל עצמו בממון חבירו שלחו ליה אסור להציל עצמו בממון חבירו אבל אתה מלך אתה [ומלך] פורץ לעשות לו דרך ואין מוחין בידו
Rav Huna stated a different explanation of the verse: There were stacks of barley belonging to Jews in which the Philistines were hiding, and David wanted to burn down the stacks to kill the Philistines and save his own life. He raised the dilemma: What is the halakha? Is it permitted to save oneself by destroying the property of another? They sent the following answer to him: It is prohibited to save oneself by destroying the property of another. But you are king, and a king may breach the fence of an individual in order to form a path for himself, and none may protest his action, i.e., the normal halakhot of damage do not apply to you since you are king.

ר' יהודה ור' יוסי הוו קא אזלי באורחא אחזיה בולמוס לר' יהודה קפחיה לרועה אכליה לריפתא א"ל ר' יוסי קפחת את הרועה

The Gemara relates that Rabbi Yehuda and Rabbi Yosei were walking on the road when Rabbi Yehuda was seized with bulmos. He overpowered a nearby shepherd and ate the bread that the shepherd had in his hand, since his life was in danger. Rabbi Yosei said to him: You have robbed that shepherd.

אפי' הוא בסכנת מות וצריך לגזול את חבירו כדי להציל נפשו צריך שלא יקחנו אלא על דעת לשלם (וע"ל סי' ש"פ ס"ג):
Even if he's in dire danger and needs to steal from his friend in order to save his soul, he should not take it unless with the intent to pay.

תשובה ותו דכתיב דרכיה דרכי נועם וצריך שמשפטי תורתינו יהיו מסכימים אל השכל והסברא

...Further, it is written [of Torah] “its ways are pleasant ways” [Proverbs 3:17] and the Torah’s laws ‎must ‎be consistent with intellect and logic.