【发布时间】:2012-02-20 21:46:30
【问题描述】:
我在 Drupal 6 .module 文件中有一个 PHP 函数。我正在尝试在执行更密集的任务(例如数据库查询)之前运行初始变量验证。在 C# 中,我曾经在 Try 块的开头实现 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();
}
}
但是,当我尝试运行代码时,它告诉我只能在 Catch 块内抛出对象。
提前致谢!
【问题讨论】:
-
tl;dr "抛出 $e->getMessage();"应该是“throw $e;”
-
似乎产生了相同的代码:
Parse error: syntax error, unexpected 'throw' (T_THROW)。我想不再支持了。
标签: php exception-handling drupal-6 try-catch