working program with exception handling