Skip to content

Latest commit

 

History

History
136 lines (110 loc) · 2.36 KB

01-Overview.md

File metadata and controls

136 lines (110 loc) · 2.36 KB

Overview

BaseException::__construct()

The basic use:

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

List of parameters:

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

    ...

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

Container-Exception:

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

The container is used to change the flag is_loggable:

    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';
        }
    }

Inheriting from the BaseException

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

    /**
     * ClassNotExist
     *
     * @param       string      $class         Class name
     */
    public function __construct($class)
    {
        parent::__construct
        (
            array
            (
                'message' => "Class '$class' does not exist",
                'class'   => $class
            )
        );
    }
}

FatalException

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

Debug data

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

BaseException static methods

Registry

StorageI