140 likes | 242 Views
IS 118 Introduction to Development Tools. Chapter 7. 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. Exception Handling Concepts.
E N D
IS 118 Introduction to Development Tools Chapter 7 IS 118
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 118
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 118
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 118
Exception class • Built-in class to help called Exception • Takes two parameters – message and code • getCode() • getMessage() • getFile() • getLine() • getTrace() • getTraceAsString() • _toString() IS 118
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().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } IS 118
User Defined Continued • class fileWriteException extends Exception • { • function __toString() • { • return 'fileWriteException '. $this->getCode() • . ': '. $this->getMessage().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } IS 118
User Defined - 3 • class fileLockException extends Exception • { • function __toString() • { • return 'fileLockException '. $this->getCode() • . ': '. $this->getMessage().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } • ?> IS 118
Bob’s Auto Parts • <?php • class fileOpenException extends Exception • { • function __toString() • { • return 'fileOpenException '. $this->getCode() • . ': '. $this->getMessage().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } IS 118
Bob’s Auto Parts - 2 • class fileWriteException extends Exception • { • function __toString() • { • return 'fileWriteException '. $this->getCode() • . ': '. $this->getMessage().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } IS 118
Bob’s Auto Parts - 3 • class fileLockException extends Exception • { • function __toString() • { • return 'fileLockException '. $this->getCode() • . ': '. $this->getMessage().'<br />'.' in ' • . $this->getFile(). ' on line '. $this->getLine() • . '<br />'; • } • } • ?> IS 118
Where does this go? • // open file for appending • try • { • if (!($fp = @fopen("orders.txt", '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 '<p>Order written.</p>'; • } IS 118
Where does it go - 2 • catch (fileOpenException $foe) • { • echo '<p><strong>Orders file could not be opened. ' • .'Please contact our webmaster for help.</strong></p>'; • } • catch (Exception $e) • { • echo '<p><strong>Your order could not be processed at this time. ' • .'Please try again later.</strong></p>'; • } IS 118
Other Error Handling • Chapter 25 discusses DEBUGGING • This is outside the scope of this course • BUT, it would be good to look at it IS 118