Moses said before God: Master of the Universe. Why is it that the righteous prosper, the righteous suffer, the wicked prosper, the wicked suffer?
God said to him: Moses, the righteous person who prospers is a righteous person, the son of a righteous person, who is rewarded for the actions of his ancestors. The righteous person who suffers is a righteous person, the son of a wicked person, who is punished for the transgressions of his ancestors. The wicked person who prospers is a wicked person, the son of a righteous person, who is rewarded for the actions of his ancestors. The wicked person who suffers is a wicked person, the son of a wicked person, who is punished for the transgressions of his ancestors.
The righteous person who prospers is a completely righteous person whose actions are entirely good and whose reward is entirely good both in this world and in the World-to-Come.
The righteous person who suffers is one who is not a completely righteous person. Because he does have some transgressions, he is punished in this world so that he will receive a complete reward in the World-to-Come.
The wicked person who prospers is one who is not a completely wicked person. God rewards him in this world for the good deeds that he performed, so that he will receive a complete punishment in the World-to-Come.
Finally, the wicked person who suffers is a completely wicked person. Since he performed absolutely no mitzvot and deserves no reward, he receives only punishment both in this world and in the World-to-Come (Maharsha).
אמר רבא, ואיתימא רב חסדא: אם רואה אדם שיסורין באין עליו יפשפש במעשיו שנא' (איכה ג, מ) "נחפשה דרכינו ונחקורה ונשובה עד ה'." פשפש ולא מצא, יתלה בבטול תורה שנאמר (תהלים צד, יב) "אשרי הגבר אשר תיסרנו יה ומתורתך תלמדנו." ואם תלה ולא מצא, בידוע שיסורין של אהבה הם שנאמר (משלי ג, יב) "כי את אשר יאהב ה' יוכיח."
Rava, and some say Rav Ḥisda, said: If a person sees that suffering has befallen him, he should examine his actions, as it is stated: “We will search and examine our ways, and return to God” (Lamentations 3:40). If he examined his ways and found no transgression for which that suffering is appropriate, he may attribute his suffering to dereliction in the study of Torah, as it is stated: “Happy is the man whom You punish, Lord, and teach out of Your law” (Psalms 94:12). And if he did attribute his suffering to dereliction in the study of Torah, and did not find anything, he may be confident that these are afflictions of love, as it is stated: “For whom the Lord loves, He rebukes” (Proverbs 3:12).
(א) ויצילהו מידם. פי' לפי שהאדם בעל בחירה ורצון ויכול להרוג מי שלא נתחייב מיתה, משא''כ חיות רעות לא יפגעו באדם אם לא יתחייב מיתה לשמים. והוא אומרו ויצילהו מידם, פי' מיד הבחירי. ובזה סתר אומרו "ונראה מה יהיו חלומותיו וגו'," כי הבחירה תבטל הדבר, ואין ראיה אם יהרגוהו כי שקר דבר:
(1) He tried to save him from them. Because a person possesses free will and desire, and is able to kill someone who does not deserve death. This is not so for wild animals, which will not harm a person if they are not liable to death towards heaven. And this is what it means "he tried to save him from them," meaning from the hands of those with free will. And this would contradict their statement "we shall see what comes of his dreams," because free will would nullify the matter, and it would not be a proof if [the brothers] killed [Joseph] that [the dreams of Joseph] were lies.
חייב אדם לברך על הרעה כשם שמברך על הטובה שנאמר (דברים ו, ה) "ואהבת את ה' אלקיך בכל לבבך וגו'," "בכל לבבך," בשני יצריך, ביצר טוב וביצר הרע. "ובכל נפשך," אפילו הוא נוטל את נפשך. "ובכל מאדך," בכל ממונך. ד"א, "בכל מאדך," בכל מדה ומדה שהוא מודד לך הוי מודה לו.
One is obligated to recite a blessing for the bad that befalls him just as he recites a blessing for the good that befalls him, as it is stated: “And you shall love the Lord your God with all your heart, with all your soul, and with all your might” (Deuteronomy 6:5). “With all your heart” means with your two inclinations, with your good inclination and your evil inclination, both of which must be subjugated to the love of God. With all your soul means even if God takes your soul. “And with all your might” means with all your money. Alternatively, “with all your might” means with every measure that He metes out to you; whether it is good or troublesome, thank Him.