PHP RFC: Support for anonymous catches

Introduction

Currently, PHP requires all catch statements to specify an Exception class and an identifier variable. This RFC proposes new language constructs to make the variable optional, and to have a fully anonymous catch-statement.

Proposal

With the recent addition of the finally keyword for try-catch statements, PHP is now in line with many other languages such as C#, Python and Ruby (and probably many others). Those languages differ from PHP in that they don't require a variable to bind the exception in, and don't even require specifying a certain Exception class.

In this case, we don't need the exception object itself as the exception type is descriptive enough of what happened, and we do not need to log anything because the operation is tried again in 3 seconds. By not specifying a variable, we avoid the impression that we do actually need it. It also helps static analysis tools (e.g. as implemented in IDEs) more accurately discover unused variables.

We can take this one step further, to also get rid of the class:

try
{$this->connectToServer();}
catch
{$this->retry(3);}

This again avoids the impression that only a specific exception is catched, where currently the least-specific Exception is used in many catch-statements, giving the indication that the handler is only for a specific type of exception, while it's not.

Note that there may only be one anonymous catch-statement, and it has to be the last one.

Advantages

Better possibilities for static analysis tools

Avoid indication of a specific exception type

Runtime needs to perform less checks. No need to update the symboltable, no need to check exception inheritance.

People from other languages such as C#, Python and Ruby expect this to work

Disadvantages

The general consensus of the discussion in the mailinglist is that fully anonymous catches may encourage bad coding practices

Backward Incompatible Changes

Adding this syntax will not have any impact on existing code.

Proposed PHP Version(s)

As this is only a minor change in the codebase, this can be included in PHP 5.NEXT

Unaffected PHP Functionality

This change only requires changing the grammer file and the ZEND_CATCH opcode has to be extended to allow for two UNUSED arguments.

Proposed Voting Choices

I think I'd be best that the two syntax additions are voted for separately.