After all this, when Josiah had prepared the temple, Neco king of Egypt went up to fight at Carchemish on the Euphrates, and Josiah went out to meet him. But he sent envoys to him, saying, “What have we to do with each other, king of Judah? I am not coming against you this day, but against the house with which I am at war. And God has commanded me to hurry. Cease opposing God, who is with me, lest he destroy you.” Nevertheless, Josiah did not turn away from him, but disguised himself in order to fight with him. He did not listen to the words of Neco from the mouth of God, but came to fight in the plain of Megiddo. And the archers shot King Josiah. And the king said to his servants, “Take me away, for I am badly wounded.” So his servants took him out of the chariot and carried him in his second chariot and brought him to Jerusalem. And he died and was buried in the tombs of his fathers. All Judah and Jerusalem mourned for Josiah.Jeremiah also uttered a lament for Josiah; and all the singing men and singing women have spoken of Josiah in their laments to this day. They made these a rule in Israel; behold, they are written in the Laments. Now the rest of the acts of Josiah, and his good deeds according to what is written in the Law of the Lord, and his acts, first and last, behold, they are written in the Book of the Kings of Israel and Judah. 2 Chronicles 35:20-27.
An otherwise great king does not stay in his lane and dies for it. You cannot just presume on God’s protection when you are warned that you may be opposing God.