MyBB Community Forums

Full Version: throw new Exception
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All.
I was reading a tutorial that explain the __get an __set methods in class

inside this tutorial the author used this sentece throw new Exception
and he did explain all the code except this one.
I tried to google it but I didn't get any simple explation for it using class.

here is the code used in the tutorial maybe that help you to help me understand it.
<?php

class PropertyObject { 
    private $_properties = array( 
                  'name' => null, 
                  'dateofbirth' => null 
               ); 
    function __get($propertyName) { 
      if(!array_key_exists($propertyName, $this->_properties)) 
        throw new Exception('Invalid property value!'); 
      if(method_exists($this, 'get' . $propertyName)) { 
        return call_user_func(array($this, 'get' . $propertyName)); 
      } else { 
        return $this->_properties[$propertyName]; 
      } 
    } 
    function __set($propertyName, $value) { 
      if(!array_key_exists($propertyName, $this->_properties)) 
        throw new Exception('Invalid property value!'); 
      if(method_exists ($this, 'set' . $propertyName)) { 
        return call_User_func( 
                  array($this, 'set'.$propertyName), 
                  $value 
                  ); 
        } else { 
          $this->_properties[$propertyName] = $value; 
        } 
      } 
   
      function setDateOfBirth($dob) { 
        if(strtotime($dob)  == -1) { 
          throw new Exception ("The date of birth must be a valid date!"); 
        } 
        $this->_properties['dateofbirth'] = $dob; 
      } 
      function sayHello() { 
         //$this->_properties['name'] and $this->_properties['dateofbirth'] 
         //will be retrieved by __get 
         print "Hi! My name is $this->name. I was born on $this->dateofbirth"; 
    } 
  } 
  $obj = new PropertyObject(); 
  $obj->name = 'Bob'; //"Bob" is assigned to $_properties['name'] by __set 
  $obj->dateofbirth = 'March 5, l977'; //setDatoOfBirth is invoked by __set 
  $obj->sayHello(); 
$obj->dateofbirth = 'blue'; //throws an exception

?>
This should help you out:
http://us3.php.net/exceptions
It's just a way of notifiying the calling function that an error was occured. The calling function would wrap the called function with a try/catch block.

try
{
    // Call the function here
}
catch (Exception $e)
{
    // If an exception was thrown in the "try" block, you will enter in here
    // You can handle the exception here (print an error, log it, or close any resources, etc)
}
1st thanks for both of you guys.
2nd correct me if I got it wrong.

it's simply mean

it'll trying catch the 1st function, if it return false (or something like that)
then it'll go throw the exception..

thanks once more
Not exactly, the try means "try this function and see if it throws", the catch means "oh, it threw. I'd can do something with that information here".

Un-caught exceptions are bad.
thats made it more clear, I wasn't got full understanding about the try/catch block.
but I think yes I'm near about understanding it now..
thanks all of you guys.