Jul 10 2007

Php exception handling

Exception Handling

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

TAGS:

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts