Added additional exception handling inside the scanner function.