PHP中的异常处理

时间:2015-01-12 09:23:49

标签: php exception exception-handling

我尝试在数据库中没有结果时抛出异常,这里是代码:

try {
    if (!$stmt->execute()) {
       throw new ErrorExeption('there is no row with that id');
    }
} catch (ErrorExeption $e) {
    echo $e->getMessage();
}

所以,当我粘贴wron Id时,我无法看到错误消息。我做错了什么?

2 个答案:

答案 0 :(得分:3)

考虑一下:

try {
    $stmt->execute();

    if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        throw new ErrorExeption('there is no row with that id');
    }
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (ErrorExeption $e) {
    echo $e->getMessage();
}

答案 1 :(得分:1)

您能否考虑检查ErrorExeptionErrorException的拼写?

因为我在本地服务器上尝试了这个,并且脚本可以捕获我们抛出的错误消息。