PHP code example of aurimasniekis / throws

1. Go to this page and download the library: Download aurimasniekis/throws library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aurimasniekis / throws example snippets




use function Throws\thr;

thr(new Exception("FooBar"));



use function Throws\thrException;

thrException('Oh now "%s" error occurred', 'Big Error'); 

thr(new Exception('FooBar'));

thrBadFunctionCallException('FooBar');

thrBadMethodCallException('FooBar');

thrDomainException('FooBar');

thrInvalidArgumentException('FooBar');

thrLengthException('FooBar');

thrLogicException('FooBar');

thrOutOfBoundsException('FooBar');

thrOutOfRangeException('FooBar');

thrOverflowException('FooBar');

thrRangeException('FooBar');

thrRuntimeException('FooBar');

thrUnderflowException('FooBar');

thrUnexpectedValueException('FooBar');

thrErrorException('FooBar');

thrError('FooBar');

thrArgumentCountError('FooBar');

thrArithmeticError('FooBar');

thrAssertionError('FooBar');

thrDivisionByZeroError('FooBar');

thrCompileError('FooBar');

thrParseError('FooBar');

thrTypeError('FooBar');

// Instead of this
if ($foo !== 'bar') {
    throw new InvalidArgumentException(
        sprintf(
            'Oh no $foo is "%s"',
            $foo
        )
    );   
}

// Much simpler
$foo !== 'bar' || thrInvalidArgumentException('Oh no $foo is "%s"', $foo);

// Instead of this
if (false === isset($array['key'])) {
    throw new InvalidArgumentException('Oh no key is missing');
} 

// Much simpler
$array['key'] ?? thrInvalidArgumentException('Oh no key is missing');