What follows is the Catholic position; from the CCC:
*310 But why did God not create a world so perfect that no evil could exist in it? With infinite power God could always create something better.174 But with infinite wisdom and goodness God freely willed to create a world “in a state of journeying” towards its ultimate perfection. In God’s plan this process of becoming involves the appearance of certain beings and the disappearance of others, the existence of the more perfect alongside the less perfect, both constructive and destructive forces of nature. With physical good there exists also physical evil as long as creation has not reached perfection.175
311 Angels and men, as intelligent and free creatures, have to journey toward their ultimate destinies by their free choice and preferential love. They can therefore go astray. Indeed, they have sinned. Thus has moral evil, incommensurably more harmful than physical evil, entered the world. God is in no way, directly or indirectly, the cause of moral evil.176 He permits it, however, because he respects the freedom of his creatures and, mysteriously, knows how to derive good from it:
Code:
For almighty God. . ., because he is supremely good, would never allow any evil whatsoever to exist in his works if he were not so all-powerful and good as to cause good to emerge from evil itself.177
312 In time we can discover that God in his almighty providence can bring a good from the consequences of an evil, even a moral evil, caused by his creatures: “It was not you”, said Joseph to his brothers, "who sent me here, but God. . . You meant evil against me; but God meant it for good, to bring it about that many people should be kept alive."178 From the greatest moral evil ever committed - the rejection and murder of God’s only Son, caused by the sins of all men - God, by his grace that “abounded all the more”,179 brought the greatest of goods: the glorification of Christ and our redemption. But for all that, evil never becomes a good. *