Extend API to allow specifying the exception class

Adds an `on_exception` argument to specify the class of exception to
catch. This provides a cleaner API to catching only specific exception
classes.

See #17