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
If you don't want to extend Exception, you can encode your array into a string:
try {
throw new Exception(serialize(['msg'=>"Booped up with %d.",'num'=>123]));
} catch (Exception $e) {
$data = unserialize($e->getMessage());
if (is_array($data))
printf($data['msg'],$data['num']);
else
print($e->getMessage());
}
You can also use json_encode
/json_decode
if you prefer.