【问题标题】:try command not being recognized in PHP 5.3 [closed]在 PHP 5.3 中无法识别 try 命令 [关闭]
【发布时间】:2013-06-23 21:45:14
【问题描述】:

无论我把它放在代码的哪个位置,它都会窒息。

    try {
        $Result = true;
    } 
    catch(exception) {
    }

当我关闭 try 命令 '}' 时出现错误。

我检查了我的 php.ini,它报告了所有错误。我是否缺少某种设置?

为什么我的 wampserver 不能识别这个命令? Dreamweaver 将该行标记为错误,并且浏览器测试在同一点阻塞。我的 wampserver 只有 6 个月大。它必须是最新的。

有什么想法吗?

【问题讨论】:

  • "the error", "it chokes" 如何发布实际错误
  • 正如我的好友 Dagon 所说,我们需要查看您收到的实际错误消息以提供帮助。

标签: php exception try-catch


【解决方案1】:

抛出的错误是未定义的关键字exception

代码应改写为:

try {
    $Result = true;
} catch(Exception $exc){
}

请注意,Exception 只是一个类型提示。

【讨论】:

    【解决方案2】:

    正确的语法是:

    try
    {
        $Result = true;
    } 
    catch(\Exception $e) {
    }
    

    Exception 只是一个 Type Hinting,它决定了要捕获哪些异常。您甚至可以为每种异常类型使用多个 catch 块和一个 try 块。

    try
    {
        $Result = true;
    } 
    catch(\Exception $e) {
        // do something
    }
    catch(\RuntimeException $e) {
        // do something else
    }
    

    阅读更多:http://php.net/manual/en/language.exceptions.php

    【讨论】:

    • @BLaZuRE 也许你告诉我为什么会出现语法错误?你在说什么?
    猜你喜欢
    • 2021-04-08
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多