Jump to content

PHP5: Handling Custom Exceptions


redbrad0

Recommended Posts

I am switching my site over to php5 version and adding into custom exceptions by looking at examples. Can someone help me figure out what I am doing wrong?

 

  try {
    $client =& new Customer($db['tickets'], $queryClient);
  } catch (TixException $ex) {  
  echo $ex->getCode(); // <---- This does return 2 which is value of REQUIRED_VARIABLE_NOT_VALID
  echo "<hr>";
  echo TixExceptionCodes::ExceptionCodeDescriptions[$ex->getCode()]; // <-- RETURNS A ERROR
  }

 

One of the class files that is called to throw the exception

  class Customer {  
    function Customer() {
      throw new TixException("Customer.class: Input Data is not valid", TixExceptionCodes::REQUIRED_VARIABLE_NOT_VALID);
    }   
  }

 

my TixException class file that is included within all the files

<?php
class TixException extends Exception
{
    // Redefine the exception so message isn't optional
    public function __construct($message, $code = 0) {
        // some code
    
        // make sure everything is assigned properly
        parent::__construct($message, $code);
    }

    // custom string representation of object */
    public function __toString() {
        return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
    }
}

class TixExceptionCodes
{
const DATABASE_UNAVAILABLE								= 0;
const DATABASE_ERROR											= 1;

const REQUIRED_VARIABLE_NOT_VALID					= 2;


  public static $ExceptionCodeDescriptions = array(
      DATABASE_UNAVAILABLE           				=> 'Success',
      DATABASE_ERROR           							=> 'An unknown error occurred',
      REQUIRED_VARIABLE_NOT_VALID           => 'Service temporarily unavailable',
  );
}
?>

 

Error returned

PHP Parse error:  syntax error, unexpected '[', expecting ',' or ';' in /home/website/public_html/customer.php on line 15

Link to comment
https://forums.phpfreaks.com/topic/139050-php5-handling-custom-exceptions/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.