PHP:错误:异常的参数错误,引发了数组异常


PHP : Error: Wrong parameters for Exception with thrown Array Exception

我使用如下数组抛出异常:

$response = array('login_email' => '<div class="warning">Your email and / or password were incorrect</div>');
throw new 'Exception($response);

我看到的是:

Error: Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])

知道吗?

Exception()不会接受数组。你需要给它一根绳子。

$response = 'Your email and / or password were incorrect.';
throw new 'Exception($response);

读取错误:

异常([string$Exception[,long$code],异常$previor=NULL]]])

如果您想以数组为参数抛出异常,可以对数组进行json_encode,使其成为字符串。

$response = array('login_email' => 'sometext', 'last_query' => 'sometext');
throw new 'Exception(json_encode($response));

还可以看看json_encode的第二个参数:您可以添加json_PRETTY_PRINT以使您的错误缩进(它更可读,但占用更多空间),或者您可以在查看json字符串时使用类似于Notepad++的json Viewer的工具来格式化它。

如果你想抛出一个包含数组的异常,那么你可以创建自己的异常类并对其进行扩展-你能在php中抛出一个数组而不是字符串作为异常吗?