[ create a new paste ] login | about

Link: http://codepad.org/zErNR7b7    [ raw code | output | fork | 1 comment ]

ninwa - PHP, pasted on Nov 6:
<?php

// Simple exception handling...
class Math
{
    static function divide($n1, $n2)
    {
        if( $n2 == 0 )
        {
            throw new Exception("Divide by zero exception");
        }

        return $n1 / $n2;
    }
}

try
{
    echo Math::divide(1,1) . "\n";
    echo Math::divide(1,0);
}
catch( Exception $e )
{
    echo $e->getMessage();
}


?>


Output:
1
2
1
Divide by zero exception


Create a new paste based on this one


Comments:
posted by ninwa on Nov 6
A simple demonstration of exception handling.
reply