One who eats forbidden food does not recite a Beracha before eating. Thus, for example, if a person neglects the Torah’s Kashrut restrictions and orders food in a non-kosher restaurant, he should not recite a Beracha before eating the food. This applies regardless of whether the food is forbidden by Torah law or by force of Rabbinic enactment. Another example is food which one steals. Since this food is forbidden, no Beracha may be recited before eating it (Rabbi Moshe Ha’levi, Birkat Hashem). Similarly, one who eats or drinks on Yom Kippur in violation of Halacha should not recite a Beracha. If a person is healthy and has no reason to eat or drink, he must not recite a Beracha over the food or drink, since he is eating or drinking in violation of the Torah.
A Beracha recited in such cases constitutes a Beracha Le’batala – a Beracha recited in vain.
An interesting question arises in the case of one who prepared to eat at a time when eating is forbidden, and after reciting the Beracha remembered that he is not permitted to eat that time. One example is where a person forgot it was a fast day, and recited a Beracha in preparation to eat. Should he eat, in violation of Halacha, so that his Beracha will not have been recited in vain? Or should he refrain from eating, despite having recited a Beracha?
The Halacha in this case depends on the situation. If this occurred on a fast day, then one should not eat the food, and should instead recite, "Baruch Shem Kebod Malchuto Le’olam Va’ed" so that the Beracha will not be considered to have been recited in vain. Hacham Bension Abba Shaul (Israel, 1923-1998) rules that this applies not only on Yom Kippur, when eating is forbidden by Torah law, but even on other fast days, when eating is forbidden by force of Rabbinic enactment. Even then, it is preferable to recite "Baruch Shem" rather than eat.
Hacham Ovadya Yosef ZT"L, on the other hand, says that on a Rabbinical fast if one forgot and made a Beracha on food, he should taste a little piece so his Beracha will not be L’batala, and then continue fasting. This would apply to fasts like Tiisha B’ab and Taanit Esther.
In other instances, however, it is preferable to eat after mistakenly reciting a Beracha at a time when eating is forbidden. For example, if a person recited a Beracha over food in the morning before praying Shaharit, and then realized that eating is forbidden at that point, he should take a small bite of the food. This is the ruling of Hacham Yishak Yosef (current Sephardic Chief Rabbi of the State of Israel), in Yalkut Yosef (vol. 1, p. 128). He explains that the prohibition against eating before praying was enacted by the Sages, and not by the Torah, whereas reciting a Beracha Le’batala is, according to some opinions, a Torah violation. Therefore, it is preferable to eat to avoid a situation of a Beracha Le’batala. Furthermore, the reason why eating before praying in the morning is forbidden is because it expresses arrogance, and eating a small morsel of food before praying does not express arrogance. For this reason, too, it is preferable to eat a small bite to avoid a Beracha Le’batala.
This applies as well in a case where one mistakenly recited a Beracha over food before Kiddush on Shabbat, or before Habdlala on Mosa’eh Shabbat, when eating is forbidden. The person should take a small bite of food to prevent his Beracha from becoming a Beracha Le’batala.
Finally, this applies also to one who mistakenly recited a Beracha over a dairy food within six hours of eating meat. The Yalkut Yosef rules that one should take a bite of the dairy food, despite the fact that eating dairy is forbidden at this time. He notes that in any event there are Halachic authorities who permit eating dairy foods within six hours of meat, as long as the meat meal has ended. Although we do not follow this view, it may be relied upon in a case where eating dairy is necessary to prevent a Beracha from becoming a Beracha Le’batala.
Summary: If a person mistakenly recited a Beracha over food on a fast day, and then remembered that eating is forbidden, he should recite "Baruch Shem Kebod Malchuto Le’olam Va’ed" and not eat. If, however, this occurred in other situations when eating is forbidden, such as before praying in the morning, or before Kiddush or Habdala, one should take a small bite of the food. Similarly, if one mistakenly recited a Beracha over a dairy food within six hours of eating meat, he should take a small bite of the dairy food.