Permissibility of haraam things in the case of necessity and the conditions governing that
Question

I know that haraam things may be permissible if it becomes necessary to do them. Are there conditions governing this ruling so that it may be applied correctly?.
Praise be to Allah.Among the basic principles of Islamic sharee’ah, on which the scholars are agreed, is that cases of necessity make forbidden things permissible. 
There is a great deal of evidence to support this principle in the Holy Qur'aan and the Prophet's Sunnah, for example, the verses in which Allah says (interpretation of the meaning):
“Forbidden to you (for food) are: Al‑Maitah (the dead animals — cattle — beast not slaughtered), blood, the flesh of swine, and that on which Allaah’s Name has not been mentioned while slaughtering (that which has been slaughtered as a sacrifice for others than Allaah, or has been slaughtered for idols) and that which has been killed by strangling, or by a violent blow, or by a headlong fall, or by the goring of horns — and that which has been (partly) eaten by a wild animal — unless you are able to slaughter it (before its death) ‑ and that which is sacrificed (slaughtered) on An‑Nusub (stone‑altars). (Forbidden) also is to use arrows seeking luck or decision; (all) that is Fisqun (disobedience of Allaah and sin). This day, those who disbelieved have given up all hope of your religion; so fear them not, but fear Me. This day, I have perfected your religion for you, completed My Favour upon you, and have chosen for you Islam as your religion. But as for him who is forced by severe hunger, with no inclination to sin (such can eat these above mentioned meats), then surely, Allaah is Oft‑Forgiving, Most Merciful”
[al-Maa’idah 5:3]
“And why should you not eat of that (meat) on which Allaah’s Name has been pronounced (at the time of slaughtering the animal), while He has explained to you in detail what is forbidden to you, except under compulsion of necessity?”
[al-An’am 6:119]
Examples of this principle include the following: 
1.Eating dead meat for one who cannot find anything else and fears that he will die of hunger.
2.Speaking words of kufr when subjected to torture and force.
3.Warding off an aggressor even if that leads to killing him.
See: al-Ashya’ wa’l-Nazaa’ir by Ibn Nujaym, p. 85 
Necessity means cases in which a person will be harmed if he does not take the haraam option, in which the harm will effect the five essentials which are: religion, life, honour, reason and wealth. 
With regard to the conditions of a haraam thing becoming permissible in the case of necessity, Shaykh Muhammad ibn ‘Uthaymeen (may Allah have mercy on him) mentioned two conditions for that and he explained them in abundant detail with examples, as well as mentioning some objections and the response to them. Hence we will limit ourselves to quoting his words. He (may Allah have mercy on him) said: 
This principle is one of the basic fiqhi principles which are indicated by sharee’ah: Everything that is forbidden becomes permissible in the case of necessity. 
So what is forbidden becomes permissible in the case of necessity, but there are two conditions. 
The first condition: 
We should be compelled to do this specific haraam thing, meaning that we cannot find anything with which to meet that necessity except this haraam thing. If we can find something else, then it does not become permissible, even though it could meet our necessity. 
The second condition: 
The necessity should be met by that haraam thing; if that is not the case, then it remains haraam. If we are not certain whether it will meet the necessity or not, then it also remains haraam. That is because doing something haraam is definitely wrong and meeting necessity by means of it is something concerning which there is doubt, so we should not transgress by doing that which is definitely haraam for the sake of something concerning which there is doubt. 
Hence the ruling varies concerning a starving man who cannot find anything but dead meat. In this case we say, Eat the dead meat. If he says that this is committing a haraam action, we say that it has become permissible because of necessity, because you have nothing else to eat apart from this and because if you eat it you will meet that necessity. 
It was said to a man: If you drink alcohol, you will be cured of sickness. In this case we say: It is not permissible for you to drink alcohol even if you are told that it will heal you from sickness. Why is that? 
Firstly, because there is no certainty that he will be healed by it; he may drink it and not be healed of sickness. We see many sick people taking beneficial medicines that they do not benefit from. 
Secondly, the sick person may recover without any treatment, by putting his trust in Allah and praying to Him, and by means of people’s prayer (du’aa’) for him and so on. This is from the point of view of reason. 
From the point of view of evidence, it is narrated in a hadeeth from the Prophet (blessings and peace of Allah be upon him) that he said: “Allaah does not put your healing in that which He has forbidden to you.” The rationale behind this hadeeth is clear, because Allah only forbade it to us because it is harmful to us, so how can that which is haraam be a healing and a remedy? 
Hence it is haraam to use haraam things as treatment, as the scholars have stated, and it cannot be said that this is a case of necessity, as some people think. 
If someone were to say that a person is choking and he has nothing except a cup of wine, so is it permissible for him to drink this cup to stop him choking? 
The answer is yes, because the two conditions are met in this case. He is compelled to use this exact thing and we are certain that the necessity will be met by it, so we would say: Drink the wine. But once the choking has stopped, he should stop drinking. 
If someone were to say that a man found some meat that had been slaughtered in the halaal manner and some meat from an animal that had died a natural death, can he eat the dead meat because he is forced to do so by necessity? 
The answer is that he cannot do that, because the necessity can be met by something else, so it is not permissible because the first condition is not met. 
If a person were to say, I am thirsty and I have nothing but a cup of wine; can I drink it? 
The answer is no, as the scholars said, because this is not a case of necessity; rather it will only make him more thirsty, so there is no benefit in transgressing and doing something haraam, because necessity will not be warded off by it and the second condition is not fulfilled. 
If a person were to say: If a sick person has no choice but to drink blood as a remedy, is it permissible for him to do that? The answer is that it is not permissible for him to do that, because the two conditions are not met. End quote. 
Sharh Manzoomah Usool al-Fiqh wa Qawaa’idihi (p. 59-61) 
And Allah knows best.