private static void ensureTopicExists(List<String> foundTopics, Optional<String> requestedTopic, Boolean requireTopicExists) {
        // If no topic name was mentioned, do not need to throw exception.
        if (requestedTopic.isPresent() && !requestedTopic.get().isEmpty() && requireTopicExists && foundTopics.isEmpty()) {
            // If given topic doesn't exist then throw exception
            throw new IllegalArgumentException(String.format("Topic '%s' does not exist as expected", requestedTopic));
        }
    }
ensures topic existence and throws exception if topic doesn't exist

@param foundTopics        Topics that were found to match the requested topic name.
@param requestedTopic     Name of the topic that was requested.
@param requireTopicExists Indicates if the topic needs to exist for the operation to be successful.
                          If set to true, the command will throw an exception if the topic with the
                          requested name does not exist.
tools/src/main/java/org/apache/kafka/tools/TopicCommand.java