Php exception handling

exception.thumbnail

In normal desktop applications , we use exception handling everywhere in our
code.
But in server side script such as php, we can limit the usage of Exception handling.

Example for raising an exception in php

<?php

//create function with an exception

function checkValue($number)
{

if($number>4)
throw new
Exception(“Value must be 4 or below“);

return true

}

//trigger exception
checkValue(5);

?>

The above code will give : Fatal error: Uncaught exception ‘Exception’

If we call like this:

<?php

try{

checkValue(5);

}

catch(Exception $e)

{
echo ‘Message: ‘ .$e->getMessage();

}

?>

The code above will get an error like this:

Message: Value must be 4 or below

2 Comments

2 Responses to “Php exception handling”

  1. Johnny April 14, 2009 at 10:08 pm #

    Great post, thanks !

  2. John Panto October 4, 2011 at 2:07 pm #

    Hi,
    this is great article !!!!!!! thanks for sharing with us.
    you may see here a another nice one article on exception handling.

    Thanks !!!!!!

Leave a Reply

More in php, php source code (96 of 108 articles)


In many cases, we , php programmers need Roman number display I II III IV etc. Here is a ...