# Filters that throw Errors

**URL:** <https://discourse.laminas.dev/t/filters-that-throw-errors/3827>\
**Category:** Components & MVC\
**Tags:** laminas-filter\
**Created:** [December 26, 2024, 2:12am UTC](https://discourse.laminas.dev/t/filters-that-throw-errors/3827 "2024-12-26T02:12:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![earthiverse](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/earthiverse/32/1660_2.png) [@earthiverse](https://discourse.laminas.dev/u/earthiverse)\
**Post date:** [December 26, 2024, 2:12am UTC](https://discourse.laminas.dev/t/filters-that-throw-errors/3827/1 "2024-12-26T02:12:22Z")

</div>

[laminas-filter’s FilterInterface’s filter()](https://github.com/laminas/laminas-filter/blob/2.40.x/src/FilterInterface.php) has

```auto
@throws Exception\RuntimeException If filtering $value is impossible.

```

but almost no filters throw exceptions.

In reality, most filters seem to follow the approach `if we can't filter it, return the value that we were provided`.

One example is [ToFloat](https://github.com/laminas/laminas-filter/blob/2.40.x/src/ToFloat.php) which says

```auto
If the value provided is non-scalar, the value will remain unfiltered

```

* * *

From the code for how most filters are written, it seems filters should be implemented so they don’t throw errors.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [December 27, 2024, 11:16am UTC](https://discourse.laminas.dev/t/filters-that-throw-errors/3827/2 "2024-12-27T11:16:20Z")

</div>

Do you have an explicit question or is it just a statement?

Filters should not throw exceptions so that subsequent validators can do their job and give appropriate messages to the user.  
A new major version for laminas-filter is in work and any help is welcome!

---

<div class="post-metadata">

**Author:** ![earthiverse](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/earthiverse/32/1660_2.png) [@earthiverse](https://discourse.laminas.dev/u/earthiverse)\
**Post date:** [December 29, 2024, 4:15am UTC](https://discourse.laminas.dev/t/filters-that-throw-errors/3827/3 "2024-12-29T04:15:46Z")

</div>

Sorry, my question was essentially “should we actually throw exceptions?”

> Filters should not throw exceptions so that subsequent validators can do their job and give appropriate messages to the user.

But this gives me my answer, thanks!

* * *

I think removing the @throws in the interface, and adding what I quoted from you above would be a good improvement.

In our project, we replaced [laminas-filter/src/DateTimeFormatter.php at 2.40.x · laminas/laminas-filter · GitHub](https://github.com/laminas/laminas-filter/blob/2.40.x/src/DateTimeFormatter.php#L70) to not throw an error.

I might have some time, I’ll take a look at the new version and see if there’s anything I might be able to help with.
