Is there any hadith that says that if a person reaches the age of forty, he will be safe from being mental illness, fungal illness, and so on?
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 is His slave and Messenger.
Perhaps you are referring to the hadeeth cited by Abu Yaʻla on the authority of Anas ibn Maalik attributing it to the Prophet which reads:
"For a newborn child, until he reaches puberty, his good deeds are written to the credit of his parents, while his bad deeds are written neither against him nor against his parents. Once he reaches puberty, the pen begins to write (his deeds) and Allaah issues His command to the two angels who accompany him to record and be strict. When he reaches forty years of age in Islam, Allaah confers upon him security from three (afflictions): madness, leprosy and vitiligo. When he reaches fifty, Allaah makes his reckoning lighter. When he reaches sixty, Allaah grants him reversion to Him as pleases Him. When he reaches seventy, the inhabitants of Heaven love him. When he reaches eighty, Allaah records his good deeds and overlooks his bad deeds. When he reaches ninety, Allaah forgives him his past and future sins, allows him to intercede on behalf of his family, and he is named 'the captive of Allaah on earth'. Then, when he is brought back to the miserable old age so that he knows nothing after having known, Allaah continues to record for him the good deeds which he used to do when he was well, and if he does a bad deed, it is not recorded."
Al-Haafith Ibn Katheer declared this hadeeth weak and commented on it, saying, "This is a very strange hadeeth and its authenticity is highly questionable." [Tafseer Ibn Katheer]
Allaah knows best.
You can search for fatwa through many choices