added exception catching and writing to log