Made more robust the management of exceptions and added a test