Add exception handling

Added exception handling and mailing.