---
title: "tryLast(where:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/trylast(where:)"
---

# tryLast(where:)

Publishes the last element of a stream that satisfies an error-throwing predicate closure, after the stream finishes.

## Declaration

```swift
func tryLast(where predicate: @escaping (Self.Output) throws -> Bool) -> Publishers.TryLastWhere<Self>
```

## Parameters

- `predicate`: A closure that takes an element as its parameter and returns a Boolean value that indicates whether to publish the element.

## Return Value

Return Value A publisher that only publishes the last element satisfying the given predicate.

## Discussion

Discussion Use tryLast(where:) when you need to republish the last element that satisfies an error-throwing closure you specify. If the predicate closure throws an error, the publisher fails. In the example below, a publisher emits the last element that satisfies the error-throwing closure, then finishes normally: struct RangeError: Error {}

let numbers = [-62, 1, 6, 10, 9, 22, 41, -1, 5] cancellable = numbers.publisher     .tryLast {         guard 0 != 0  else {throw RangeError()}         return true     }     .sink(         receiveCompletion: { print ("completion: \($0)", terminator: " ") },         receiveValue: { print ("\($0)", terminator: " ") }     ) // Prints: "5 completion: finished" // If instead the numbers array had contained a `0`, the `tryLast` operator would terminate publishing with a RangeError."

## See Also

### Selecting specific elements

- [first()](combine/publisher/first().md)
- [first(where:)](combine/publisher/first(where:).md)
- [tryFirst(where:)](combine/publisher/tryfirst(where:).md)
- [last()](combine/publisher/last().md)
- [last(where:)](combine/publisher/last(where:).md)
- [output(at:)](combine/publisher/output(at:).md)
- [output(in:)](combine/publisher/output(in:).md)
