Exception

Posted in PHP by

PHP Exceptions allow you finer control over your applications. They are very similar to java or c++ exceptions.

Exceptions can be thrown and caught. Here is a small example:

<php
function doSomething(){
 ...//some code
if($something == false)
 throw new Exception('Awesome Error Message');
...//more code. but won't get executed!
}

try{ //code doSomething(); //more code, but this code won't get executed } catch( Exception $e){ die($e->getMessage()); //Prints Awesome Error Message }

Pretty easy, and very useful. Use it whenever it makes sense. Since PHP is friendly..it won't annoy you, if a function thorws an exception and you do not catch it.

The object $e prodives methods for the line of exception and more.

Published at , Updated at 2011-10-25

next: Speed up your Website prev: Secure User Input with Markdown