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';
}
}
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
)
);
}
}
class MyFatalException extends BaseException
{
// This exception has aspect: "fatal"
protected $is_fatal = true;
}
class MyException extends BaseException
{
public function __construct($object)
{
$this->set_debug_data($object);
parent::__construct('its too bad!');
}
}