Added all kinds of checks of exceptions.

This is probably still racy, and I'll probably have to test everything
toroughly.