syntax error, finally block is not recognized

2019-08-09 18:39发布

问题:

Get syntax error, unexpected '{'. When I delete, finally block it works. that is a GET method of RESTful service.

Route::resource('braining/firmware', 'Braining\FirmwareController',
                    ['only' => ['index']]); 

Controller

<?php
namespace App\Http\Controllers\Braining;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class FirmwareController extends Controller
{
    public function index() {
        try {
        }
        catch(Exception $e) {
        }
        finally {
        }
    }
}

回答1:

Upgrade PHP to version 5.5 and the problem will go away.

In PHP 5.5 and later, a finally block may also be specified after or instead of catch blocks. Code within the finally block will always be executed after the try and catch blocks, regardless of whether an exception has been thrown, and before normal execution resumes.

Source: http://php.net/manual/en/language.exceptions.php#language.exceptions.finally



回答2:

You must be using PHP 5.4 or lower which does not have the finally implemented yet. Either remove it or upgrade the PHP to 5.5