I'm throwing the exception using the array like so:
$response = array('login_email' => '<div class="warning">Your email and / or password were incorrect</div>');
throw new \Exception($response);
and what I'm catching is:
Error: Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])
Any idea?
Throwing Exceptions in PHP Defined in the constructor, and accessed via the getMessage() method, the message is the human-readable error that can often be related to the end user.
The getMessage() method returns a description of the error or behaviour that caused the exception to be thrown.
The following keywords are used for PHP exception handling. Try: The try block contains the code that may potentially throw an exception. All of the code within the try block is executed until an exception is potentially thrown. Throw: The throw keyword is used to signal the occurrence of a PHP exception.
Exception() won't take an array. You need to give it a string.
$response = 'Your email and / or password were incorrect.';
throw new \Exception($response);
Read the error:
Exception([string $exception [, long $code [, Exception $previous = NULL]]])
If you want to throw an exception with an array as parameter, you can json_encode your array so it becomes a string.
$response = array('login_email' => 'sometext', 'last_query' => 'sometext');
throw new \Exception(json_encode($response));
Have a look also at the second parameter of json_encode: you can add JSON_PRETTY_PRINT to have you error indented (it's more readable but it takes more space), or you can use a tool like JSON Viewer for Notepad++ to format your json string when looking at it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With