PHPackages                             sanmai/trycatch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. sanmai/trycatch

AbandonedArchivedLibrary[Debugging &amp; Profiling](/categories/debugging)

sanmai/trycatch
===============

Exception-handling callable wrapper

v0.3.2(5y ago)012.2k↑35%[1 PRs](https://github.com/sanmai/trycatch/pulls)MITPHPPHP ^7.0 || ^8.0

Since May 12Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sanmai/trycatch)[ Packagist](https://packagist.org/packages/sanmai/trycatch)[ RSS](/packages/sanmai-trycatch/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (6)Versions (8)Used By (0)

TryCatch
========

[](#trycatch)

[![Latest Stable Version](https://camo.githubusercontent.com/d1a8219ff755afcc22811e7d41a2336101b8a27b106bf752b5f9a6682fa4e9e6/68747470733a2f2f706f7365722e707567782e6f72672f73616e6d61692f74727963617463682f762f737461626c65)](https://packagist.org/packages/sanmai/trycatch)[![Build Status](https://camo.githubusercontent.com/6f071820d7bd7c7cd3ecebd27b52d379bc48eca7eb109e4f02959dabe8982877/68747470733a2f2f7472617669732d63692e6f72672f73616e6d61692f74727963617463682e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/sanmai/trycatch)[![Coverage Status](https://camo.githubusercontent.com/0b93a4a0a3b1bb734dbff0d9e149901e82d6070160f6aedc18a6ab359dd1a9b2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73616e6d61692f74727963617463682f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/sanmai/trycatch?branch=master)

Exception-handling callable wrapper for Closures and other callables.

Usage
-----

[](#usage)

Suppose you were passing a closure to configure some dependencies, or do another mundane task:

```
$object->setCallback($someCallback);
```

And then you suddenly find that your callback started throwing exceptions here and there. You have the callback from somewhere beyond your control, so you can't really change what it does.

Now, if you want to handle certain types of exceptions gracefully, this is how you could do that:

```
$retval = $object->setCallback(\TryCatch\TryCatch::wrap($yourCallback)
  ->whenFailed(function (\Exception $e, $a, $b) {
    if ($e instanceof SpecificException) {
        // handle this one gracefully
        return null;
    } elseif ($a > $b) {
        // else check for rogue callback's arguments
    }

    throw $e;
});
// $retval will be null in case of SpecificException being caught
```

You can also peek into arguments, that were passed to the callback.

[See tests for for additional examples.](tests/TryCatchTest.php)

Install
-------

[](#install)

```
composer require sanmai/trycatch
```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 87.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~338 days

Recently: every ~414 days

Total

6

Last Release

1967d ago

PHP version history (3 changes)v0.1PHP ~5.6|~7.0

v0.3PHP ^7.0

v0.3.2PHP ^7.0 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/edcb8dde95c71b1c97c3c91e57d3548795fa2014c657744fb878e2be3b5949fc?d=identicon)[sanmai](/maintainers/sanmai)

---

Top Contributors

[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (34 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (5 commits)")

---

Tags

phpexception

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sanmai-trycatch/health.svg)

```
[![Health](https://phpackages.com/badges/sanmai-trycatch/health.svg)](https://phpackages.com/packages/sanmai-trycatch)
```

###  Alternatives

[filp/whoops

php error handling for cool kids

13.2k402.4M1.4k](/packages/filp-whoops)[laracraft-tech/laravel-xhprof

Easy XHProf setup to profile your laravel application!

235321.4k](/packages/laracraft-tech-laravel-xhprof)[thehocinesaad/laravel-error-ai

This package adds Ask AI button to the error page.

2214.4k](/packages/thehocinesaad-laravel-error-ai)[bavix/laravel-xhprof

Quick profiling of your code for Laravel

22156.6k](/packages/bavix-laravel-xhprof)[h4cc/phpqatools

A meta composer package for PHP QA Tools.

6418.6k1](/packages/h4cc-phpqatools)[ikkez/f3-falsum

Pretty error handling for Fat-Free Framework

21145.8k3](/packages/ikkez-f3-falsum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
