我在 Drupal 6中有一个 PHP 函数。模块文件。我试图在执行更密集的任务(比如数据库查询)之前运行初始变量验证。在 C # 中,我曾经在 Tryblock 的开头实现 IF 语句,如果验证失败,它会抛出新的异常。抛出的异常将在 Catch 块中捕获。下面是我的 PHP 代码:
function _modulename_getData($field, $table) {
try {
if (empty($field)) {
throw new Exception("The field is undefined.");
}
// rest of code here...
}
catch (Exception $e) {
throw $e->getMessage();
}
}
However, when I try to run the code, it's telling me that objects can only be thrown within the Catch block.
先谢谢你!