Exceptions in PHP5

Introduction

This article explains the PHP 5 exception statements throw, try and catch. PHP 5 has an associated exception model almost like that of other programming languages. Exceptions are often throw and caughtin PHP. Code is also surrounded in an successive attempt blocks, to facilitate the catching of potential exceptions. Each attempt should have a minimum of one corresponding catch block. Multiple catch blocks can accustom catches of completely different categories of exceptions. Traditional execution (when no exception is thrown at intervals of the attempt block, or once a catch matching the thrown exception's category isn't present) can continue at the last catch block defined in the sequence. Exceptions are often thrown (or re-thrown) at intervals of a catch block. When an associated exception is thrown, the code following the statement won't be dead, and PHP can determine the primary matching catch block. If an associated exception is not caught then a PHP Fatal Error is issued with an associated "Uncaught Exception ..." message, unless a handler has been outlined with set_exception_handler(). In PHP 5.5 and later, a finally block may additionally be provided after the catch blocks. Code at intervals the finally block can perpetually be dead once the try to catch blocks, despite whether or not an associated exception has been thrown, and before traditional execution resumes.

The thrown object should be associated with an instance of the Exception class or a subclass of the Exception class. Throw an object that can not lead to a PHP Fatal Error. The internal PHP function is mainly used for error reporting.

Example

The following is an example of throwing an exception:

<?php

function modulus($x) {

    if (!$x) {

        throw new Exception('Division by zero.');

    }

    return 1%$x;

}

try {

    echo modulus(5) . "<br>";

    echo modulus(0) . "<br>";

} catch (Exception $m) {

    echo 'exception: '

       $m->getMessage(), "<br>";

}

//execution continue

echo "hi guy...";

?>

 

Output

122299 - Copy (3).jpg

 

Example

 

This example shows an exception handling of the finally block. The finally block can support only PHP versions 5.5 and above.

 

<?php

function modulus($x) {

    if (!$x) {

        throw new Exception('Division by zero.');

    }

    return 1%$x;

}

try {

    echo modulus(5) . "\n";

} catch (Exception $m) {

    echo 'exception: ',  $m->getMessage(), "<br>";

} finally {

    echo "finally one"."<br>";

}

try {

    echo modulus(0) . "<br>";

} catch (Exception $m) {

    echo 'exception: ',  $m->getMessage(), "<br>";

} finally {

    echo "finally two"."<br>";

}

echo "Hi guy...";

?>

 

And in the next example I'll show nested exceptions.

 

<?php

class ya extends exception { }

class demo {

    public function trying() {

        try {

            try {

                throw new ya('hello!');

            } catch (ya $m) {

                // Rethrow it

                throw $m;

            }

        } catch (exception $m) {

            var_dump($m->getMessage());

        }

    }

}

$hello = new demo;

$hello->trying();

?>
 

Output

 122299 - Copy (2).jpg

Example

 

The following is another example:

 

<?php

$age=20;

try{

 if($age>18){

echo 'old enough.';

}else{

 throw new exception ('not old enough');

 }

} catch (Exception $ex){

 echo 'Error: '.$ex->getmessage();

}

?>

 

Output

122299 - Copy.jpg


Example

The following is another example:

<?php

$age=17;

try{

 if($age>18){

echo 'old enough.';

}else{

 throw new exception ('not old enough');

 }

} catch (Exception $ex){

 echo 'Error: '.$ex->getmessage();

}

?>

 

Output

122299.jpg