Errors

From KnowledgeTree Document Management Made Simple

Jump to: navigation, search

Exceptions or Errors are an essential part of programming. Eventually, something will go wrong - whether its that you can't open a file you should be able to, or whether the option a user "selected" isn't available.

Dealing with that in KT is done with the PEAR::Error class. Using this is straightforward, and can be demonstrated with a quick piece of code.


 ...

 function testme($my_value) {
      if (!is_int($my_value)) {
          return PEAR::raiseError("Please give me a number to work with");
      } else {
          return $my_value * 10;
      } 
 }

 $new_val = testme("boo");
 if (PEAR::isError($new_val)) {
     printf ("Couldn't do it, captain:  %s", $new_val->getMessage());
 } else {
    printf("I had the power, captain!  Answer is %d", $new_val);
 }

Personal tools