Table of Contents Previous Next
Logo
Client-Side Slice-to-PHP Mapping : 28.13 Exception Handling
Copyright © 2003-2010 ZeroC, Inc.

28.13 Exception Handling

Any operation invocation may throw a run-time exception (see Section 28.10) and, if the operation has an exception specification, may also throw user excep­tions (see Section 28.9). Suppose we have the following simple interface:
exception Tantrum {
    string reason;
};

interface Child {
    void askToCleanUp() throws Tantrum;
};
Slice exceptions are thrown as PHP exceptions, so you can simply enclose one or more operation invocations in a try-catch block:
$child = ...        // Get child proxy...

try
{
    $child>askToCleanUp();
}
catch(Tantrum $t)
{
    echo "The child says: " . $t>reason . "\n";
}
Typically, you will catch only a few exceptions of specific interest around an oper­ation invocation; other exceptions, such as unexpected run-time errors, will usually be handled by exception handlers higher in the hierarchy. For example:
function run()
{
    $child = ...          // Get child proxy...
    try
    {
        $child>askToCleanUp();
    }
    catch(Tantrum $t)
    {
        echo "The child says: " . $t>reason . "\n";
        $child>scold();  // Recover from error...
    }
    $child>praise();     // Give positive feedback...
}

try
{
    // ...
    run();
    // ...
}
catch(Ice_Exception $ex)
{
    echo $ex>__toString() . "\n";
}
This code handles a specific exception of local interest at the point of call and deals with other exceptions generically. (This is also the strategy we used for our first simple application in Chapter 3.)

Table of Contents Previous Next
Logo