Question: what is the best way to check whether a stack exists using the AWS Java SDK, given a stack name?

I've tried the below code based on - <https://github.com/aws/aws-sdk-java/blob/master/src/samples/AwsCloudFormation/CloudFormationSample.java>

```
    DescribeStacksRequest wait = new DescribeStacksRequest();
    wait.setStackName(stackName);        
    List<Stack> stacks = awsCFTClient.describeStacks(wait).getStacks();
    if (stacks.isEmpty()) {
        logger.log("NO_SUCH_STACK");
        return true;
    }

```

However, I am getting:

AmazonServiceException:com.amazonaws.services.cloudformation.model.AmazonCloudFormationException: Stack with id "stackName" does not exist.

Thanks in advance!

Answer: 
In case someone else is looking for a quick and dirty solution, this works,

```
//returns true if stack exists
public boolean stackExists(AmazonCloudFormation awsCFTClient, String stackName) throws Exception{
    DescribeStacksRequest describe = new DescribeStacksRequest();
    describe.setStackName(stackName);
    //If stack does not exist we will get an exception with describe stack
    try {
        awsCFTClient.describeStacks(describe).getStacks();
    } catch (Exception e) {
        logger.log("Error Message: " + e.getMessage());
        if (e.getMessage().matches("(.*)" + stackName + "(.*)does not exist(.*)")) {
            return false;
        } else {
            throw e;
        }
    }
    return true;
}

```

If there is a better way for doing this, please let me know.
Found a slightly better way to do it but with a few remarks:

* aws java sdk 2.0 is used here <https://github.com/aws/aws-sdk-java-v2>
* in the example below, you see kotlin syntax

`client.listStacks().stackSummaries().any{ it.stackName() == stackName }`

In Java you can simply replace `any` method `anyMatch`.

According to the shallow check of the former aws java sdk 1.0 there is also `listStacks()` method in AmazonCLoudFormationClient class which does the same.

But in my case, I needed to filter those stacks (because if the stack has status DELETED... it will still appear in the list above) based on the status. So probably overloaded method will be more useful:

```
@Override
    public ListStacksResult listStacks(ListStacksRequest request) {
        request = beforeClientExecution(request);
        return executeListStacks(request);
    }

```