Modified exception handling in API and added tests of new behaviour.