The Bible

When I, in turn, have been hostile to them and have removed them into the land of their enemies, then at last shall their obdurate heart humble itself, and they shall atone for their iniquity.
(Leviticus 26:42)

Isaiah 12:1: And you shall say on that day, “I thank You, G-d, for You were angry with me; may Your anger subside and may You comfort me.” Commentary of Rashi: For You were angry with me, and You sent me to exile, and my exile atoned for me, and now my sin has been expiated.
(Isaiah 12:1)

The Midrash

“Behold, I will bring them from the northern land, and gather them from the farthest places of the earth, among them the blind and the lame, the pregnant and childbearing together – a great assembly will return here”
(Jeremiah 31:7).

“the blind and the lame” are a metaphor for ignorant Jews who have not studied Torah and are not careful with observing the commandments, and yet will be redeemed because of the few basic laws they keep. Their redemption is only possible after the atonement of exile, and therefore even after the Jewish people has repented, G-d says to them: “My children, sit for a while until the time for the banquet arrives.” The purpose is to purge their sins so that they might all be pure and enter life in the world to come.
(Tanna Devei Eliyahu 14:1)

G-d promised Abraham
(Genesis 13:16),

“I will make your seed like the dust of the earth.” …And just as dust is trod upon, so too your descendents will be trod upon by the gentiles. This is the meaning of the words
(Isaiah 51:23),

“And I will place it in the hand of those who melted you, who said to your soul, bend down and let us pass over you.” Even so, it is for your good, for they will clean away your sins
(Genesis Rabbah 41:9)

Rabbi Yitzchak Arama, author of Akeidas Yitzchak (1420-1494)
[יעקב אמר לעשיו] “Let my master pass before his servant, and I will continue slowly, according to the work that is before me and according to the children, until I come to my master, to Seir”
(Genesis 33:14).

The entire verse is a metaphoric description of Jewish history. Jacob said, “I have much work to do! My children cannot reach their full potential unless they go through the refinery of trials, suffering and exile three times (i.e. Egypt, Babylonia and Rome). Only then will their souls be purified and ready to absorb spiritual good, to be the chosen people and to live peacefully forever. This refining process will take a long time, and will end with the coming of the messiah, when I will come to you at Seir.”
(Akeidas Yitzchak Shaar 26)

Rabbi Yosef Yabetz, Spanish Rabbi (15th-16th century)
When G-d told Abraham about the exiles that would befall his descendents, why did he not pray to annul the decree? Even to save the sinners of Sodom he made valiant efforts
(Genesis 18:23-33),

so why not for his own children? He answers that he could not pray since he foresaw with his prophecy the great purpose of the exiles, and that without them it would be impossible for his children to inherit the World to Come.
This is the meaning of Isaiah 42:22-43:1: “But they are a despised and downtrodden people, all emaciated young men, hidden in prisons; they were despoiled and no one saved them, downtrodden and no one said, desist…And now, so says G-d, your Creator, O Jacob, do not fear, for I have redeemed you…” After the exile and the suffering are over, the sins will be atoned and Israel will merit reward for bearing the suffering. Similarly, the prophet Hoshea said (7:16), “And I afflicted them, strengthening their arms, but they considered it bad of Me.” Israel complains about the exiles and the afflictions, but does not realize their positive purpose. They are like the man whom the doctor told, “You will die unless you cut off your finger,” and he called a surgeon to cut it off, and the man complained and yelled at the doctor for causing him the pain of the operation – is there anyone so foolish?
(Chasdei Hashem, pp. 16-17)

Rabbi Moshe Alshich, Rabbi of Safed, Palestine (1508–1593)
“Serve G-d with fear, and rejoice with trembling”}
(Psalms 2:11).

If you live in fear of the government, let that not deter you from serving G-d, but on the contrary, serve G-d with that fear, for when you accept your difficult exile as an atonement for sin, it will be considered serving G-d. “Rejoice with trembling” – rejoice if you tremble in fear of the government, for then the atonement will be greater. Besides gaining atonement, the merit of your faith and pureness of heart will be your strongest weapon against them.
(Romemos Keil)

Rabbi Chaim Vital, Rabbi of Safed, Palestine (1542-1620)
Our servitude and exile is for the purpose of cleansing our sins.
(Etz Hadaas Tov, Vayishlach)

Rabbi Yishaya Horowitz, author of the Shelah (1565-1630) 
The Midrash tells us that there were four Jewish exiles – Babylonia, Media, Greece and Rome. The exiles are alluded to in the first verses of Genesis, and they were shown to Abraham at the Covenant Between the Parts
(Genesis 15).

Why did these unhappy events have to be mentioned at such times? Seemingly, at a time when the world or the Jewish people is being built, the Torah should focus on the good, saving punishments and misfortune for another time.
The answer is that since man is imperfect, the exiles are a necessary element of the building of the world and of the Jewish people. Just as in the laws of Shabbos, a destructive act is considered work only if it leads directly to a constructive act, so too the exile is ultimately not destructive but accomplishes the purification of the Jewish people and of the entire world, to prepare it for the purpose of creation, the messianic era.
(Shelah, Lech Lecha)

I will exalt You, O G-d, for You have lifted me up (dilisani)
(Psalms 30:2).

The root letters daled-lamed have two meanings – poverty and uplifting – because the poverty, the misfortunes of exile, are the reason for future uplifting. The Jewish people are children of G-d (Deuteronomy 14:1),

and even when G-d is angry with us and brings destruction, it is for our own good. “As a man chastises his son, G-d chastises you.”
(Deuteronomy 8:5)

The purpose of the misfortunes is the purification in the refinery of the exile, to destroy sin and be clean, without impurities, for the future.
(Shelah, Balak)

The Purpose of Exile: Spreading Knowledge of G-d in the World

Rabbi Shmuel Eidels (Maharsha), Talmudic commentator (1555-1631)
“For like the four winds sides of the world I have scattered you (זכרי’ ב’ י’) Just as the world cannot exist without winds, so the world cannot exist without the Jewish people.”
(Talmud, Avodah Zarah 10b)

The world exists because the Jewish people are scattered around the world, making known the existence of G-d and His Torah.
(Maharsha, commentary to Avodah Zarah 10b)

The Midrash

After the sin of the spies, when G-d decreed that the Jewish people would wander in the desert for forty years, He said, “However, as I live, and let the glory of G-d fill the entire earth”
(Numbers 14:21).

The words “glory of G-d” refer to the Jewish people, as it says, “All that is called by My name, I created for My glory”
(Isaiah 43:7).

Thus, He decreed that the Jewish people must be scattered throughout the entire earth.
(Pesikta, quoted in Yalkut Reuveini on Numbers 14:21)