PHP5 Exception handling
prev | next

What's happening? - Internals

Using Exceptions in your code is very simple. All you have to do is 'throw' some Exception. This can either be an Exception defined by yourself, or a generic one.

class fish
{
    function swim($speed)
    {
        // critical error, fish could explode if faster
        if($speed>120) throw new Exception('swim limit reached.');
    }
}


// new fish
$turbofish = new fish;

// try to speedup
try
{
    // try to be a turbo-fish
    $turbofish->swim(121);
}
catch (Exception $e)
{
	// go deeper, to gain speed
    $turbofish->changeDepth(1500);
}
 
As you can see, catching this Exception results in the ability to do several things, we can let the fish speedup until its limits are reached and if this happens, we can take countermeasures.

In this example, we're still not using the full power of Exception Handling. As we want to be able to use the same Error Handling in other situations, we'll define some custom exceptions, which can then be used by different objects for similar tasks.
prev | next