Aviv Ronen Blog

Jun 23 2008

Simple PHP Exception Handling explanation

What is an Exception

Finaly, with php5 came a new object oriented way for dealing errors,
if a specified error (exceptional) condition occurs exception handling is used to change the
flow of the code execution .

Exceptions are a great tool and should only be used with error conditions,
dont ever use them to jump to another place in the code at a specified
point - you cant know what will happend.

Basic Use of php5 Exceptions

When an exception is thrown, PHP will try to find the matching “catch” block.

The bad way:

<?php
 function checksum($number1=NULL,$number2=NULL)
   {
       if($number1===NULL OR $number2===NULL)
          {
  throw new Exception("Value must not be NULL");
 }
   return true;
  }
 checksum(7,NULL);
 ?>

The good way:

Good Luck! use this php5 tool smartly.