Reduce the number of conditionals and catch the exception.