Source of: /ch04/example.4-10.php
<?php

function formatException(Exception $e)
{
    return 
"Error {$e->getCode()}: {$e->getMessage()}
        (line: {$e->getline()} of {$e->getfile()})"
;
}


function 
average($total$n)
{
    if (
$n == 0)
        throw new 
Exception("Number of items = 0"1001);

    return 
$total $n;
}


// Script that uses the average() function
try
{
    
$a average(1000);
    print 
"Average = {$a}";
}
catch (
Exception $error)
{
    print 
formatException($error);
}

?>