replaced try-catch with if-else block