Gemara: Rav Yehuda said in the name of Rav about the pasuk, "Inform me of my end and what the measure of my life is" (Tehillim 39:5) – David said before Hashem: "Inform me of my end." Hashem responded: "It is My decree that we do not inform flesh and blood about his end." [David continued]: "What is the measure of my life?" [Hashem responded:] "It is My decree that we do not inform a person the measure of his life."
Ein Ayah: There are two potential ways to investigate the length of a person’s life. One is to figure out, either through intellect or prophecy, the extent of a person’s power, which would give an indication as to how long he could continue living. However, Hashem, in His wisdom, arranged that the way a person uses his strengths is so complicated and that so much is dependent on things that are outside a person’s body, that one will never succeed in determining life expectancy in this way. This is how the gemara understands David’s request to know his end and Hashem’s answer that He arranged the world in a manner that man will not succeed.
The logic for not knowing how long one will live is simple. Study of biology is open to all sorts of people, including those with serious moral lacking. For such people to not feel the weight of uncertainty as to when they will die is morally damaging. Indeed, the lack of confidence weakens people’s evil inclination, even when they claim to not be impacted by it. Therefore, Hashem closed the gate of advanced inquiry in these realms. In fact, Hashem did not make this knowledge available, even through the medium of prophecy, in order to help with ethical concerns. This is because if there were prophecy on the matter, then there would by necessity be some intellectual way to get hold of some of that information.
There is another, loftier way to know when one will die, which depends on great divine wisdom. Each thing in the world comes with a plan and a goal, which, among other things, determines until when it is needed. A person has a certain time allotted to him so that he can accomplish certain things. His days are set according to what he is designed to accomplish. This is what is hinted at by the phrase "ba bayamim" (literally, coming with the days, which refers to a very old person). This means that he comes with the actions that he accomplished during his life.
Someone who is so spiritually sensitive as to know what he is supposed to accomplish should be able to figure out when he is supposed to finish them and die. To do this, one must be very in tune with Divine Providence as it relates to him, which happens only when one is beyond lowly physical desires and is especially prepared to serve Hashem in a complete manner and with a love of truth.
However, Hashem decreed that even a great man will not be ethically well-served to remove himself from doubt about his future, which escapes revelation either through the physical side or the goal-related side. The doubt distances people from being overly wicked and even from being overly pious (see Kohelet 7, 16-17). The knowledge of the end of one’s life takes away from the form and value of life itself, and Hashem prevented one from acquiring this knowledge in its varying forms.