1 / 14

IS 118 Introduction to Development Tools

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.

marged
Download Presentation

IS 118 Introduction to Development Tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. IS 118 Introduction to Development Tools Chapter 7 IS 118

  2. 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

  3. 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

  4. 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

  5. Exception class • Built-in class to help called Exception • Takes two parameters – message and code • getCode() • getMessage() • getFile() • getLine() • getTrace() • getTraceAsString() • _toString() IS 118

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

More Related