dongren4758 2017-02-21 09:07 采纳率: 0%
浏览 215
已采纳

try catch块中未捕获的错误[重复]

This question already has an answer here:

I am trying to write "universal" function to execute SQL query and return JSON and handle error within. And during process there is something I don't understand - why try catch block doesn't handle error. (Later I will improve logic - question is NOT about that, but purely about error handling).

Here is my code:

public
function sqlToJSON($query, $type = array(), $params = array())
{
    try {
        $data = array();
        $stmt = $this->mysqli->prepare($query);
        if (count($type) > 0 && count($params) > 0) {
            call_user_func_array(array($stmt, "bind_param"), array_merge(array($type), $params));
        }
        $stmt->execute();
        $result = $stmt->get_result();
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
        $stmt->free_result();
        $stmt->close();
        return array('result' => 'success', 'error' => null, 'data' => $data);
    } catch (Exception $e) {
        $this->sqlError = 'Caught exception: ' . $e->getMessage();
        return array('result' => 'failed', 'error' => $e->getMessage(), 'data' => null);
    }
}

How I call this function:

    $q = 'INSERT INTO receivers (receiver_name, owner) VALUES (UPPER(?), ?)';
    $params = array(&$receiverName, &$clientEmail);
    $this->sqlToJSON($q, 'ss', $params);

This gives following expected error:

Uncaught Error: Call to a member function fetch_assoc() on boolean in ...

What I don't understand why catch block is not executed in this case?

</div>
  • 写回答

1条回答 默认 最新

  • dqy92287 2017-02-21 09:12
    关注

    In php errors are not exceptions. If you want to catch them, write your own error_handler. http://php.net/manual/en/function.set-error-handler.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记