I want to throw an array as an exception in php, instead of a string. Is it possible to do this if you define your own class that extends the Exception class?
For exampl
I think I'm a bit too late with an answer, but I wanted to share my solution as well. There are probably more people looking for this :)
class JsonEncodedException extends \Exception
{
/**
* Json encodes the message and calls the parent constructor.
*
* @param null $message
* @param int $code
* @param Exception|null $previous
*/
public function __construct($message = null, $code = 0, Exception $previous = null)
{
parent::__construct(json_encode($message), $code, $previous);
}
/**
* Returns the json decoded message.
*
* @param bool $assoc
*
* @return mixed
*/
public function getDecodedMessage($assoc = false)
{
return json_decode($this->getMessage(), $assoc);
}
}