All perfect praise be to Allaah, The Lord of the Worlds. I testify that there is none worthy of worship except Allaah, and that Muhammad, sallallaahu ‘alayhi wa sallam, is His slave and Messenger.
If performing a religiously considerable benefit leads to committing a religiously considerable harm, in a way that the benefit cannot be performed without committing the harm, then the matter has three cases which the jurists have mentioned:
1- The benefit is greater and the harm is minor or least expected, in this case, the benefit comes in priority and it should be sought even by committing this harm. The jurists determined that the preponderant benefit comes in priority over a lesser harm. For example, freeing Muslim prisoners from the enemy by paying them a ransom is a preponderant benefit which comes in priority over the lesser harm which is that the enemy will benefit from the ransom paid to them in return for freeing the prisoners.
2- The harm is greater than the benefit, in this case, avoiding the harm comes in priority over achieving the benefit. In this case the benefit is not sought in order to avoid the harm; this is what the scholars call “repelling harm comes in priority over achieving benefits.”
3- The benefit and harm are equal: this case is like the previous one, meaning that repelling harm comes in priority over achieving the benefit as per the previous rule.
Therefore, if two matters contradict, one of which is benefit and the other is harm, then you may compare and balance doing one of them according to what we have mentioned.
Allaah Knows best.