PHP code example of ifcastle / exceptions

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

    

ifcastle / exceptions example snippets


class MyException extends \Exceptions\BaseException
{
    protected string $template      = 'The template error message with {var}';

    public function __construct($var)
    {
        parent::__construct
        ([
             'var'         => $this->toString($var)
         ]);
    }
}

$exception = new MyException('string');

// should be printed: The template error message with 'string'
echo $exception->getMessage();


use \Exceptions\Registry;
use \Exceptions\LoggableException;

Registry::resetExceptionLog();

$exception      = new LoggableException('this is a loggable exception');

$log            = Registry::getExceptionLog();

if($log[0] === $exception)
{
    echo 'this is loggable $exception';
}


    throw new BaseException('message', 0, $previous);

    // use array()
    $exception = new BaseException
    ([
        'message'     => 'message',
        'code'        => 0,
        'previous'    => $previous,
        'mydata'      => [1,2,3]
    ]);

    ...

    // print_r([1,2,3]);
    print_r($exception->getExceptionData());



    try
    {
        try
        {
            throw new \Exception('test');
        }
        catch(\Exception $e)
        {
            // inherits data Exception
            throw new BaseException($e);
        }
    }
    catch(BaseException $exception)
    {
        // should be printed: "test"
        echo $exception->getMessage();
    }



    try
    {
        try
        {
            // not loggable exception!
            throw new BaseException('test');
        }
        catch(\Exception $e)
        {
            // log BaseException, but don't log LoggableException
            throw new LoggableException($e);
        }
    }
    catch(LoggableException $exception)
    {
        // echo: "true"
        if($exception->getPrevious() === $e)
        {
            echo 'true';
        }
    }



try
{
    dispatch_current_url();
}
catch(BaseException $myException)
{
    $myException->appendData(['browser' => get_browser()]);

    // and throw exception on...
    throw $myException;
}


class ClassNotExist  extends BaseException
{
    // This exception will be logged
    protected bool $isLoggable = true;

    /**
     * ClassNotExist
     *
     * @param       string      $class         Class name
     */
    public function __construct(string $class)
    {
        parent::__construct
        ([
             'template'    => 'Сlass {class} does not exist',
             'class'       => $class
        ]);
    }
}

class MyFatalException  extends BaseException
{
    // This exception has aspect: "fatal"
    protected bool $isFatal    = true;
}

class MyException  extends BaseException
{
    public function __construct($object)
    {
        $this->setDebugData($object);
        parent::__construct('its too bad!');
    }
}