我在Drupal 6 .module文件中有一个PHP函数。我试图在执行更密集的任务(如数据库查询)之前运行初始变量验证。在C#中,我在我的Try块的开头实现了IF语句,如果验证失败,会抛出新的异常。抛出的异常将被捕获到Catch块中。以下是我的PHP代码:在PHP Try Catch块中抛出异常
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“throw $ e-> getMessage();”应该是“扔$ e”; – 2016-07-04 18:40:21