Skip to content

Getting Started

Throw provides a fluent, readable API for conditionally throwing exceptions in Laravel applications.

Throw requires PHP 8.5+.

Install Throw with composer:

Terminal window
composer require cline/throw

Add Throw’s trait to your custom exception classes:

use Cline\Throw\Concerns\ConditionallyThrowable;
use RuntimeException;
class InvalidTokenException extends RuntimeException
{
use ConditionallyThrowable;
public static function expired(): self
{
return new self('Token has expired');
}
}

Now you can use fluent conditional throwing:

// Throw if condition is true
InvalidTokenException::expired()->throwIf($token->isExpired());
// Throw unless condition is true
InvalidTokenException::expired()->throwUnless($token->isValid());