This post will be an example of James' Thoughts and Ramblings, but it will have some good stuff. I promise.
In II Kings 23:29, the righteous king Josiah is killed by Pharaoh Neco of Egypt.
The Chronicler had a problem with this story, for he believed that God rewards the righteous and punishes the wicked. In his mind, if Josiah died in a battle, then he must have deserved it. And so the Chronicler has an explanation: God warned Josiah through Pharaoh Neco not to fight the Egyptian army, since Neco wasn't specifically coming against Judah (II Chronicles 35:20-26). But Josiah let his pride get in the way, and he died as a result.
The thing is that the author of II Kings (the Deuteronomist) also believed in divine retribution. In Deuteronomy 7, God promises to bless Israel if she obeys him, and to curse her if she disobeys. And the schema of blessings and curses applies to individual Israelites, not just the collective. Deuteronomy 24 says, for example, that God will bless those who return their debtor's cloak and leave sheaves for the poor to gather. Yet, in the case of Josiah, the Deuteronomist presents an incident in which a righteous man suffers an evil, without any attempt to explain how that's fair. Why? Did the Deuteronomist believe that divine retribution was mostly a collective issue?
A similar issue: How do I-II Maccabees handle theodicy? Do they believe that life is fair because God is just, and God consistently rewards the righteous and punishes the wicked?
Yes and no. On the "no" side, righteous people die. The righteous priest, Onias III, is deprived of his priesthood and killed (II Maccabees 4). Judas the Maccabee perishes in the battle of Elasa (I Maccabees 9). Jonathan is kidnapped and murdered by the Seleucid Trypho (I Maccabees 13). And Simon and his sons are slaughtered after being lured to a banquet (I Maccabees 16). A righteous lifestyle doesn't mean invulnerability, as far as the authors of Maccabees are concerned. Moreover, the nation of Israel never gets completely free from oppression, at least in the Book of I Maccabees.
On the "yes" side, many of the wicked characters get their come-uppance. The Hellenizing priest Jason slaughters his own people, and he ends up fleeing from city to city, with nobody to welcome him. He dies in exile, unburied and unmourned (II Maccabees 5:1-10). Andronicus, the murderer of Onias III, is executed by Antiochus Epiphanes, who occasionally did the right thing (II Maccabees 4:30-38). God also motivated Antiochus to put to death Menelaus, a corrupt high priest, by having him thrown down a tower (II Maccabees 13:3-8). And one story relates that Antiochus Epiphanes got a horrible disease, prompting him to acknowledge the God of Israel at the end of his life (II Maccabees 9).
Also on the "yes" side, there's a belief that Antiochus' oppression of Israel was God's punishment of his people for succumbing to Hellenism and neglecting his law (II Maccabees 4:7-17; 5:17; 7:18). Yet, on the "no" side, even after many Israelites fought for God's law, they still had to wrestle with enemies. But, on the "yes" side, a poem in I Maccabees 14:4-15 presents life under Simon Maccabeus as a paradise.
In I Maccabees, the Maccabees win battle after battle, often against overwhelming odds. On one occasion, however, 2,000 righteous Israelites fall in battle. I Maccabees 5:57-62 says that they died for not listening to Judas' instructions in their zeal for glory, and also because they "did not belong to the family of those men through whom deliverance was given to Israel" (v 62, NRSV). In this case, the author tries to explain away why bad things happen. But I-II Maccabees don't really do this for the deaths of Judas, Jonathan, and Simon.
Theodicy is hard because life is not neat. There are many times when the righteous suffer while the wicked prosper. Yet, the wicked coming to a bad end does not exactly surprise me, since wickedness sometimes contains the seeds of its own destruction. Plus, there's always the possibility that even innocent people will suffer when God punishes a nation. And our own pride and foolishness can often lead to our downfall, as it did for Josiah and those 2,000 Israelites. Yet, this doesn't necessarily limit God, for Jacob botched a lot of things up, and his life turned out all right in the end (sort of).
Moreover, God doesn't always intervene on behalf of each and every individual. A puzzling passage is Deuteronomy 20:5-6, where it's acknowledged that Israelites can actually die in battle, even when God is with the Israelite army. But shouldn't each and every Israelite soldier be invincible? It doesn't necessarily work that way. So is it really a surprise that Judas died in battle?
But God still fought on the side of the Maccabees, and Judaism was preserved from forces that tried to destroy it. This fulfilled God's purposes, for God wanted to build on the foundation of the Torah when Jesus Christ came to earth.