11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 8 • ERROR AND EXCEPTION HANDLINGwww.it-ebooks.info1,Could not connect to the database!2,Incorrect password. Please try again.3,Username not found.4,You do not possess adequate privileges to execute this command.When MyException is instantiated with a language and an error code, it will read in the appropriatelanguage file, parsing each line into an associative array consisting of the error code and itscorresponding message. The MyException class and a usage example are found in Listing 8-2.Listing 8-2. The MyException Class in Actionclass MyException extends Exception {}function __construct($language,$errorcode) {$this->language = $language;$this->errorcode = $errorcode;}function getMessageMap() {$errors = file("errors/".$this->language.".txt");foreach($errors as $error) {list($key,$value) = explode(",",$error,2);$errorArray[$key] = $value;}return $errorArray[$this->errorcode];}try {throw new MyException("english",4);}catch (MyException $e) {echo $e->getMessageMap();}Catching Multiple ExceptionsGood programmers must always ensure that all possible scenarios are taken into account. Consider ascenario in which your site offers an HTML form that allows the user to subscribe to a newsletter bysubmitting his or her e-mail address. Several outcomes are possible. For example, the user could do oneof the following:• Provide a valid e-mail address• Provide an invalid e-mail address• Neglect to enter any e-mail address at all• Attempt to mount an attack such as a SQL injection186

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!