PHP code example of artex / exceptions

1. Go to this page and download the library: Download artex/exceptions 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/ */

    

artex / exceptions example snippets


use Artex\Exceptions\ArtexRuntimeException;

try {
    throw new ArtexRuntimeException("Something went wrong!", 500);
} catch (ArtexRuntimeException $e) {
    echo $e->getMessage();
}

use Artex\Exceptions\ArtexException;

try {
    throw new ArtexException(
        "Validation failed!",
        422,
        ['field' => 'email', 'error' => 'Invalid format']
    );
} catch (ArtexException $e) {
    // Access additional context
    var_dump($e->getContext());
}

namespace MyApp\Exceptions;

use Artex\Exceptions\ArtexRuntimeException;

class CustomException extends ArtexRuntimeException
{
    public function __construct(string $message = "", int $code = 0, array $context = [])
    {
        parent::__construct($message, $code, $context);
    }
}

use Artex\Exceptions\ArtexRuntimeException;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Set up a PSR-3 logger
$logger = new Logger('example');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));

// Register the logger
ArtexRuntimeException::registerLogger($logger);

try {
    throw new ArtexRuntimeException("Critical failure", 500);
} catch (ArtexRuntimeException $e) {
    // Exception is automatically logged
    echo "Exception logged: " . $e->getMessage();
}

namespace MyApp\Exceptions;

use Artex\Exceptions\ArtexRuntimeException;

class CustomException extends ArtexRuntimeException
{
    public function __construct(string $message = "", int $code = 0, array $context = [])
    {
        parent::__construct($message, $code, $context);
    }
}