2012-12-03 43 views
4

我想學習PHP,我剛搬到例外,當我 從終於例外給出了一個錯誤的PHP

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

Example #2 Exception handling with a finally block

嘗試一個例子,我得到一個錯誤

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'; 
+3

您使用PHP 5.5或更高版本? –

+0

您使用的是什麼版本的PHP? – xylar

+1

終於阻止由php 5.5或更高版本支持 – vlcekmi3

回答

20

在PHP 5.5中添加了finally try-catch塊,它仍在開發中,所以它可能不適合您的原因是因爲您使用的是PHP 5.4或更早版本。

除非您將它們恢復到早期的PHP版本或者您位於5.5版本,否則您將無法最終使用。

+0

好吧,謝謝我使用5.4.4並幫助我嘗試關閉此問題 – Rafee

相關問題