php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62164 [PATCH] Add support for alternative try/catch syntax
Submitted: 2012-05-26 06:59 UTC Modified: 2015-03-21 20:36 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:2 of 4 (50.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: kriss at krizalys dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.4.3 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kriss at krizalys dot com
New email:
PHP Version: OS:

 

 [2012-05-26 06:59 UTC] kriss at krizalys dot com
Description:
------------
The attached patch adds support to alternatively open a try block using a colon (:) in try/catch structures. When using this syntax, each catch block must also be open by a colon, try and catch blocks must not be closed by a closing brace (}), and the whole try/catch structure must be closed by a "endtry" keyword.

The idea is to provide greater readability when PHP code is intermixed with HTML code and keep consistency with constructs like if/endif, for/foreach, etc...

Please, note that after patching, the Zend scanner and parser, as well as ext/tokenizer_data.c should be regenerated (using re2c, bison and tokenizer_data_gen.sh respectively, or using the provided Makefile targets).



Test script:
---------------
<?php
try:
	echo "Trying something unsafe\n";
	throw new Exception('thrown using alternative try/catch syntax');
catch (int $e):
	echo "Caught exception of type int (ints are not throwable, this is just example)\n";
	// several statements can also be used here
catch (Exception $e):
	echo "Caught exception of type Exception: {$e->getMessage()}\n";
	// several statements can also be used here
endtry;
?>
<?php try:?>
<p>Trying something unsafe</p>
<?php throw new Exception('thrown using alternative try/catch syntax');?>
<?php catch (int $e):?>
<p>Caught exception of type int (ints are not throwable, this is just example)</p>
<?php catch (Exception $e):?>
<p>Caught exception of type Exception: <?php echo $e->getMessage();?></p>
<?php endtry;?>

Expected result:
----------------
Trying something unsafe
Caught exception of type Exception: thrown using alternative try/catch syntax
<p>Trying something unsafe</p>
<p>Caught exception of type Exception: thrown using alternative try/catch syntax</p>

Actual result:
--------------
PHP Parse error:  syntax error, unexpected ':', expecting '{' in /home/aaa/test.php on line 2

Parse error: syntax error, unexpected ':', expecting '{' in /home/aaa/test.php on line 2


Patches

php-5.4.3-alt-try-catch.diff (last revision 2012-05-26 07:00 UTC by kriss at krizalys dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-29 03:17 UTC] kriss at krizalys dot com
I tested the patch successfully on Gentoo x86_64/PHP-5.4.3
 [2015-03-21 20:36 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2015-03-21 20:36 UTC] nikic@php.net
Closing as Won't Fix, as I don't think PHP is interested in supporting alternative syntax for more control structures. If you really think this feature is necessary, please submit an RFC (or at least a discussion on the PHP internals mailing list).
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jan 15 04:01:28 2025 UTC