---
title: "tryFilter(_:)"
framework: combine
role: symbol
role_heading: Instance Method
path: "combine/publisher/tryfilter(_:)"
---

# tryFilter(_:)

Republishes all elements that match a provided error-throwing closure.

## Declaration

```swift
func tryFilter(_ isIncluded: @escaping (Self.Output) throws -> Bool) -> Publishers.TryFilter<Self>
```

## Parameters

- `isIncluded`: A closure that takes one element and returns a Boolean value that indicated whether to republish the element or throws an error.

## Return Value

Return Value A publisher that republishes all elements that satisfy the closure.

## Discussion

Discussion Use tryFilter(_:) to filter elements evaluated in an error-throwing closure. If the isIncluded closure throws an error, the publisher fails with that error. In the example below, tryFilter(_:) checks to see if the element provided by the publisher is zero, and throws a ZeroError before terminating the publisher with the thrown error. Otherwise, it republishes the element only if it’s even: struct ZeroError: Error {}

let numbers: [Int] = [1, 2, 3, 4, 0, 5] cancellable = numbers.publisher     .tryFilter{         if $0 == 0 {             throw ZeroError()         } else {             return $0 % 2 == 0         }     }     .sink(         receiveCompletion: { print ("\($0)") },         receiveValue: { print ("\($0)", terminator: " ") }      )

// Prints: "2 4 failure(DivisionByZeroError())".

## See Also

### Filtering elements

- [filter(_:)](combine/publisher/filter(_:).md)
- [compactMap(_:)](combine/publisher/compactmap(_:).md)
- [tryCompactMap(_:)](combine/publisher/trycompactmap(_:).md)
- [removeDuplicates()](combine/publisher/removeduplicates().md)
- [removeDuplicates(by:)](combine/publisher/removeduplicates(by:).md)
- [tryRemoveDuplicates(by:)](combine/publisher/tryremoveduplicates(by:).md)
- [replaceEmpty(with:)](combine/publisher/replaceempty(with:).md)
- [replaceError(with:)](combine/publisher/replaceerror(with:).md)
