Sayyiduna Abu Huraira (may Allah be pleased with him) reports that the Messenger of Allah (peace and blessings be upon him) said: "When any of you sees someone afflicted with a trial, he should say: '«الْحَمْدُ لِلّٰهِ الَّذِي فَضَّلَنِي عَلَيْكَ وَعَلَىٰ كَثِيرٍ مِنْ عِبَادِهِ تَفْضِيلًا»' 'All praise is due to Allah who has favored me over you and over much of His creation.'"
Related hadith on this topic