PHPackages                             sandromiguel/php-throw - 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. sandromiguel/php-throw

ActiveProject

sandromiguel/php-throw
======================

PHP package for throwing custom exceptions with ease.

v1.1.0(2y ago)146[1 PRs](https://github.com/SandroMiguel/php-throw/pulls)MITPHPPHP &gt;=8.1

Since Mar 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SandroMiguel/php-throw)[ Packagist](https://packagist.org/packages/sandromiguel/php-throw)[ Docs](https://github.com/SandroMiguel/php-throw)[ RSS](/packages/sandromiguel-php-throw/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (8)Used By (0)

PhpThrow
========

[](#phpthrow)

PhpThrow (Beta) is a PHP package for throwing custom exceptions with ease.

Features
--------

[](#features)

- Provides custom exception classes with additional functionality.
- Easy to use and integrate into existing projects.

Installation
------------

[](#installation)

You can install PhpThrow via Composer:

```
composer require sandromiguel/php-throw
```

Usage
-----

[](#usage)

### Comparing Native and PhpThrow exceptions

[](#comparing-native-and-phpthrow-exceptions)

The following code shows how to use the native `InvalidArgumentException` class and the `ThrowInvalidArgumentException` class to throw an exception if the provided value is less than zero:

#### Native `InvalidArgumentException`:

[](#native-invalidargumentexception)

```
try {
    $value = -5;

    if ($value < 0) {
        throw new InvalidArgumentException('The value must be greater than or equal to 0.');
    }

    // ...
} catch (InvalidArgumentException $e) {
    echo $e->getMessage();
}
```

#### `ThrowInvalidArgumentException`:

[](#throwinvalidargumentexception)

```
try {
    $value = -5;

    ThrowInvalidArgumentException::ifZeroOrNegative($value);

    // ...
} catch (ThrowInvalidArgumentException $e) {
    echo $e->getMessage();
}
```

### ThrowInvalidArgumentException

[](#throwinvalidargumentexception-1)

`ThrowInvalidArgumentException` extends the built-in `InvalidArgumentException` and provides helper methods for throwing exceptions related to invalid arguments.

#### create()

[](#create)

`create($message)`: Creates a new instance of the exception with the given message.

```
$email = 'not-a-valid-email-address';

try {
    // Validate the email address
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        throw ThrowInvalidArgumentException::create('Invalid email address');
    }

    // ...
} catch (ThrowInvalidArgumentException $e) {
    echo $e->getMessage(); // Output: Invalid email address
}
```

#### ifZeroOrNegative()

[](#ifzeroornegative)

`ifZeroOrNegative($value, $message = null)`: Throws an exception if the numeric value is less than 0.

```
try {
    ThrowInvalidArgumentException::ifZeroOrNegative(-1);
} catch (\PhpThrow\ThrowInvalidArgumentException $e) {
    echo $e->getMessage(); // Output: The value must be greater than or equal to 0.
}
```

#### ifZeroOrNegativeWithValue()

[](#ifzeroornegativewithvalue)

`ifZeroOrNegativeWithValue($value, $message = null)`: Throws an exception if the numeric value is negative, including the value in the error message.

```
try {
    ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(-1);
} catch (\PhpThrow\ThrowInvalidArgumentException $e) {
    echo $e->getMessage(); // Output: The value -1 must be greater than or equal to 0.
}
```

You can also use a custom message without the placeholder:

```
ThrowInvalidArgumentException::ifZeroOrNegativeWithValue(
    -1,
    'Value must be positive'
); // Output: Value must be positive. Provided -1
```

### ThrowPageNotFoundException

[](#throwpagenotfoundexception)

`ThrowPageNotFoundException` is used to handle exceptions when a page is not found.

Example: `ThrowPageNotFoundException::forPage`

```
use PhpThrow\ThrowPageNotFoundException;

$page = 'non-existent-page';
ThrowPageNotFoundException::forPage($page); // Throws an exception with the message "The page 'non-existent-page' was not found."
```

Note: By default, the error code is set to 404. You can change it using the `code` parameter.

Contributing
------------

[](#contributing)

Want to contribute? All contributions are welcome. Read the [contributing guide](CONTRIBUTING.md).

Questions
---------

[](#questions)

If you have questions tweet me at [@sandro\_m\_m](https://twitter.com/sandro_m_m) or [open an issue](../../issues/new).

Changelog
---------

[](#changelog)

See [CHANGELOG.md](CHANGELOG.md)

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

TODO
----

[](#todo)

- Implement additional exception classes and methods for common validations

\*\*~ sharing is caring ~\*\*

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.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 ~1 days

Total

3

Last Release

770d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6b082015ff3afef14c40ed5af8887ccaf378c6eaabd15aac38103ecdb5b0dc4f?d=identicon)[sandromiguel](/maintainers/sandromiguel)

---

Top Contributors

[![SandroMiguel](https://avatars.githubusercontent.com/u/6423157?v=4)](https://github.com/SandroMiguel "SandroMiguel (16 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

phpexceptions

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sandromiguel-php-throw/health.svg)

```
[![Health](https://phpackages.com/badges/sandromiguel-php-throw/health.svg)](https://phpackages.com/packages/sandromiguel-php-throw)
```

###  Alternatives

[pestphp/pest-plugin-stressless

Stressless plugin for Pest

67792.6k16](/packages/pestphp-pest-plugin-stressless)

PHPackages © 2026

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