Background
No exception handling mechanism exists today. There are certain errors that can be raised
and processed by a registered error handler but there isn’t structured exception handling
on the language level.
Need
Exception handling is a very nice tool when used correctly (it should be used to catch
errors and not to control regular program flow). It is often useful to developers to be able
to protect a big block of code with a try/catch construct so that each error doesn’t have to
be handled on each line where an error could occur but in general for the complete code
block. The reason why this often makes sense is because if not all of the code executes
well you might just want to print an error message and it saves writing error handling
code on each line where something could go wrong.
Overview
The proposed implementation of try/throw/catch will look similar as other popular
programming languages. Any code which is inside a try/catch block can throw an
exception passing the exception to the closest exception handler. If this exception handler
doesn’t want to take care of the exception it can re-throw the exception.
Internal functions (usually written in C) will also be able to raise exceptions, however,
they will most probably continue to return error codes as return values and not as
exceptions. Most developers will probably not use exceptions and the current error
reporting of internal functions is quite good.
Functionality
The currently suggested syntax is as follows:
1 try {
2 …code3 if (failure) {
4 throw new MyException(“Failure”);
5}
6 …code
7 } catch ($exception) {
8 … handle exception
9
10 throw $exception; // Re-throw exception.
11 }
The code in the try/catch block starts at line 2 and ends at line 6. Any exception which is
raised inside this block is handled by the catch statements (unless there is a try/catch
block which is closer in scope. Any kind of value can be thrown and when the exception
reaches the catch statements it will be accessibly via the $exception variable which the
developer can define (on line 7 $exception could for instance be exchanged with
$foobar).
When an exception value is thrown it is thrown by-value. The exception will skip all code
and unwind the function stack until it finds the closest catch. The exception handling
code can check $exception, decide if it wants to handle it or not and either handle it or re-
throw it at the end in order to make it propagate to the next catch() in line.
NOTE: This feature may cause memory leaks in certain situations. Therefore, developers
should make sure that they aren’t using it for flow-control but only for structured
exception handling in applications which don’t have a long life time (such as web
applications).
Compatibility notes
No compatibility problems exist, as this feature doesn’t exist in previous versions of the
scripting engine. In order to simplify error handling in the existing code base, the engine
will support a mode in which errors (such as E_WARNING and E_NOTICE) will raise
exceptions, instead of displaying an error. This will allow users to use one try..catch statement to recover from any possible errors during the course of a large code block
(e.g., establishing a connection to a database server, selecting a database, and issuing a
query), without having to add lots of error-handling code.
Dependencies of feature
No dependencies.