Just as one may not chase an animal in order to trap it, so too one may not exploit the opportunity to catch an animal that got stuck in a confined space. Therefore, if a deer enters a house, the door may not be closed behind it. If a bird flies in through a window, the window may not be shut behind it (Shabbat 106b; SA 316:5). If one wishes to close the door or window in order to protect against thieves or the cold, the animal must first be chased out of the house.
If the household members are having trouble chasing out the animal or bird because it is hiding and eluding pursuit, if necessary the door or window may be closed. This is because the person doing so does not intend to trap the animal or bird, but only to protect the house from thieves or the cold. Besides, even after the door or window is closed, the animal is not truly trapped, because capturing it is still an effort.
Similarly, if a window screen has flies on it, and one wishes to close the window beyond it in order to prevent the heat or cold from entering, he must first chase away the flies so that they are not trapped between the window and the screen. If it is difficult to do so, the window may be closed even while they are still there, since one does not intend to trap the flies, only to protect against the heat or cold. Besides, the flies are not truly trapped; even if he wants to catch them, he would have to make an effort to do so.
Similarly, one who wants to close a small box that has flies in it should chase them away before closing it. If it is difficult to chase them all away, he should leave something between the cover and the box in order to create an escape hatch for the flies. If necessary, the box may be closed even though a fly will be trapped inside it, since his intention is not to trap the fly, but only to close the box. Besides, the fly is not truly trapped; even if he wants to catch it, when he opens the box it may very well escape.
. In order to explain this halakha, we must first point out that in all the cases mentioned here, the trapping involved is only rabbinically prohibited, for the following reasons:1) The area is large enough that the animals are not truly trapped there. (If the box is very small, then Sefer Ha-Teruma maintains that the flies are considered trapped, while Tur maintains that they are not trapped since they can escape when the box is opened.)
2) Flies and the like are not species that are hunted. Thus doing so is only rabbinically prohibited
3) Since the goal is to close the house or box and not to trap anything, this is a melakha she-eina tzerikha le-gufah, which according to most poskim is only rabbinically prohibited.
In all the cases mentioned above, the intention is not to trap but simply to close the window. As a result, these are examples of psik reisha de-lo niĥa lei in cases of a double rabbinic prohibition. It is generally agreed that in such instances, one can be lenient when necessary. In cases of true necessity, we are lenient for a psik reisha even if there is only one rabbinic prohibition (as explained above in ch. 9 n. 2; see MB 316:15, SHT ad loc. 18, and the upcoming footnote). Accordingly, Ĥayei Adam 30:2 and MB 316:5 state that if a bird flies into a house and is located in an area large enough that trapping the bird would only be rabbinically prohibited, one may close a window or door on account of the cold. Additionally, one may bring into consideration the following opinion of Rashba (Shabbat 107a) based on the Yerushalmi. Even though the Sages state: “If a deer enters a house and one locks it in, he is liable” (Shabbat 106b), nevertheless Rashba writes that as long as his intention in closing the door is to protect the house, it is not prohibited. Even though we do not follow this Rashba, we can combine his opinion with other mitigating factors to support leniency. See Harĥavot.