finally exception gives an error php [closed]

断了今生、忘了曾经 提交于 2020-01-14 11:45:50

问题


I am trying to learn PHP, and i just moved to Exceptions and when i try a example from

http://php.net/manual/en/language.exceptions.php

Example #2 Exception handling with a finally block

And i get an error

Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\test\filename.php on line 13

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

try {
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
    echo "Second finally.\n";
}

// Continue execution
echo 'Hello World';

回答1:


The finally block of try-catch was added in PHP 5.5 which is still in development so the likely reason it isn't working for you is because you are using PHP 5.4 or earlier.

You won't be able to use finally unless they backport it into an earlier PHP version or you are on a 5.5 release.



来源:https://stackoverflow.com/questions/13690393/finally-exception-gives-an-error-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!