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 example throw new CustomException('string', $options = array('params'));
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 example throw new CustomException('string', $options = array('params'));
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 :)
Sure. It will just be up to your error handling code to be aware of, and make use of the array property appropriately. You can define your custom exception class's constructor to take any parameters you want, and then just be sure to call the base class's constructor from within the constructor definition, eg:
Then, in your calling code...
Have a look at the php docs for extending the exception class:
http://php.net/manual/en/language.exceptions.extending.php
If you don't want to extend Exception, you can encode your array into a string:
You can also use
json_encode
/json_decode
if you prefer.Yes, you can. You will need to extend the Exception class and create a __construct() method to do what you want.