added exception handling for the main while-loop