According to the Catholic Church, why does God hurt others to punish David in 2 Samuel:24? Why does God hurt innocent people to punish King David? Moreover, even David doesn't understand it, raising the additional problem of why God would give a punishment not understood by the person supposed to be punished.

From the lectionary - January 31, 2018 :


  He asked: "Do you want a three years' famine to come upon your land, or to flee from your enemy three months while he pursues you, or to have a three days' pestilence in your land? Now consider and decide what I must reply to him who sent me." David answered Gad: "I am in very serious difficulty. Let us fall by the hand of God, for he is most merciful; but let me not fall by the hand of man." Thus David chose the pestilence. Now it was the time of the wheat harvest when the plague broke out among the people. The LORD then sent a pestilence over Israel from morning until the time appointed, and seventy thousand of the people from Dan to Beer-sheba died. But when the angel stretched forth his hand toward Jerusalem to destroy it, the LORD regretted the calamity and said to the angel causing the destruction among the people, "Enough now! Stay your hand." The angel of the LORD was then standing at the threshing floor of Araunah the Jebusite. When David saw the angel who was striking the people, he said to the LORD: "It is I who have sinned; it is I, the shepherd, who have done wrong. But these are sheep; what have they done?
Why does God hurt innocent people to punish King David?
The people were not innocent. According to the Law,

Exodus 30:12 (KJV)
12 When thou takest the sum of the children of Israel after their number, then shall they give every man a ransom for his soul unto the Lord, when thou numberest them; that there be no plague among them, when thou numberest them.

The mandate is an individual one, so during a census, each person is responsible to give a sacrafice to account for his soul (life). No such sacrifice was undertaken during David's census, and so while David was the king and elected to undertake the census contrary to the Law, it was not only he that broke the law, but he led the whole nation into sin.

2 Chronicles 12:3 And Joab answered, The Lord make his people an hundred times so many more as they be: but, my lord the king, are they not all my lord's servants? why then doth my lord require this thing? why will he be a cause of trespass to Israel?
ibid. v.7
And God was displeased with this thing; therefore he smote Israel.

Why God would give a punishment not understood by the person supposed to be punished.
According to the covenant of the Law into which Israel entered with Yahweh, ignorance and innocence are not synonymous, even -perhaps especially, for the nation as a whole. Sins due to ignorance must still be atoned for.

Leviticus 4: 13-15 KJV
And if the whole congregation of Israel sin through ignorance, and the thing be hid from the eyes of the assembly, and they have done somewhat against any of the commandments of the Lord concerning things which should not be done, and are guilty: When the sin, which they have sinned against it, is known, then the congregation shall offer a young bullock for the sin, and bring him before the tabernacle of the congregation. And the elders of the congregation shall lay their hands upon the head of the bullock before the Lord: and the bullock shall be killed before the Lord.

In short, they should have known. The fact that they did not, if they did not, does not excuse their trespass. It appears, however, that at least Joab of the king's officers was aware, and warned David, and David continued with his command to take the census in a manner not ordained by the Law.