Tuesday 10th July 2007

by Sajith M.R

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

· · · ◊ ◊ ◊ · · ·

One Response to “Php exception handling”

  1. Johnny says:

    Great post, thanks !

· · · ◊ ◊ ◊ · · ·

Leave a Reply

وظائف 2011 تحويل العملات برامج برنامج تسريع التحميل برنامج الفلاش برنامج محول الصوتيات عربي hotel 2011 زيادة رواتب العساكر 1431