IS 1181 IS 118 Introduction to Development Tools Chapter 7
IS 1182 Things to Cover Exception Handling Exception Handling Concepts Structures Try, throw, catch The Exception Class User Defined Exceptions Exceptions in Bob’s Auto Parts Other Error handling
IS 1183 Exception Handling Concepts Execute code inside of a TRY block Try { // code to execute } Within this code there needs to be a throw throw new Exception (‘message’, code) And outside the code a catch catch (typehint exception)
IS 1184 Concepts - continued So: <?php try { // some code throw new Exception(‘A terrible error has occurred’, 42); } catch (Exception $e) { echo ‘Exception ‘. $e->getCode(). ‘; ‘. $e->getMessage(0.’ in ‘. $e->getFile(). ‘ on line ‘.$e->getline(). ‘,br />’; } ?>
IS 1185 Exception class Built-in class to help called Exception Takes two parameters – message and code getCode() getMessage() getFile() getLine() getTrace() getTraceAsString() _toString()
IS 1186 User Defined Exceptions Extend the base class Exception and add your own <?php class fileOpenException extends Exception { function __toString() { return 'fileOpenException '. $this->getCode() . ': '. $this->getMessage().' '.' in ' . $this->getFile(). ' on line '. $this->getLine() . ' '; }
IS 1187 User Defined Continued class fileWriteException extends Exception { function __toString() { return 'fileWriteException '. $this->getCode() . ': '. $this->getMessage().' '.' in ' . $this->getFile(). ' on line '. $this->getLine() . ' '; }
IS 1188 User Defined - 3 class fileLockException extends Exception { function __toString() { return 'fileLockException '. $this->getCode() . ': '. $this->getMessage().' '.' in ' . $this->getFile(). ' on line '. $this->getLine() . ' '; } ?>
IS 1189 Bob’s Auto Parts <?php class fileOpenException extends Exception { function __toString() { return 'fileOpenException '. $this->getCode(). ': '. $this->getMessage().' '.' in '. $this->getFile(). ' on line '. $this->getLine(). ' '; }
IS Bob’s Auto Parts - 2 class fileWriteException extends Exception { function __toString() { return 'fileWriteException '. $this->getCode(). ': '. $this->getMessage().' '.' in '. $this->getFile(). ' on line '. $this->getLine(). ' '; }
IS Bob’s Auto Parts - 3 class fileLockException extends Exception { function __toString() { return 'fileLockException '. $this->getCode(). ': '. $this->getMessage().' '.' in '. $this->getFile(). ' on line '. $this->getLine(). ' '; } ?>
IS Where does this go? // open file for appending try { if (!($fp 'ab'))) throw new fileOpenException(); if (!flock($fp, LOCK_EX)) throw new fileLockException(); if (!fwrite($fp, $outputstring, strlen($outputstring))) throw new fileWriteException(); flock($fp, LOCK_UN); fclose($fp); echo ' Order written. '; }
IS Where does it go - 2 catch (fileOpenException $foe) { echo ' Orders file could not be opened. '.'Please contact our webmaster for help. '; } catch (Exception $e) { echo ' Your order could not be processed at this time. '.'Please try again later. '; }
IS Other Error Handling Chapter 25 discusses DEBUGGING This is outside the scope of this course BUT, it would be good to look at it