Exceptions

When an error occurs, the IMu PHP API throws an exception. The exception is an IMuException object. This is a subclass of PHP's standard Exception class.

For simple error handling all that is usually required is to catch the exception and report the exception as a string:

try
{
    // ⋯
}catch (Exception $e)
{
    echo "Error: $e";
    exit(1);
}

The IMuException class overrides the Exception class's __toString method (which is called "magically" when the object is used as a string) and returns an error message.

To handle specific IMu errors it is necessary to check the exception is an IMuException object before using it. The IMuException class includes a property called id. This is a string and contains the internal IMu error code for the exception. For example, you may want to catch the exception raised when an IMuSession object's connect method fails and try to connect to an alternative server:

$mainServer = 'server1.com';
$alternativeServer = 'server2.com';
$session = new IMuSession;
$session->host = $mainServer;
try
{
    $session->connect();
}
catch (IMuException $e)
{
    // Check for specific SessionConnect error
    if ($e->id != 'SessionConnect')
    {
        echo "Error: $e";
        exit(1);
    }
    $session->host = $alternativeServer;
    try
    {
        $session->connect();
    }
    catch (Exception $e)
    {
        echo "Error: $e";
        exit(1);
    }
}
/*
 * By the time we get to here the session is connected to either the main
 * server or the alternative.
 */