Download presentation
Presentation is loading. Please wait.
1
IS 1181 IS 118 Introduction to Development Tools Chapter 7
2
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
3
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)
4
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 />’; } ?>
5
IS 1185 Exception class Built-in class to help called Exception Takes two parameters – message and code getCode() getMessage() getFile() getLine() getTrace() getTraceAsString() _toString()
6
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() . ' '; }
7
IS 1187 User Defined Continued class fileWriteException extends Exception { function __toString() { return 'fileWriteException '. $this->getCode() . ': '. $this->getMessage().' '.' in ' . $this->getFile(). ' on line '. $this->getLine() . ' '; }
8
IS 1188 User Defined - 3 class fileLockException extends Exception { function __toString() { return 'fileLockException '. $this->getCode() . ': '. $this->getMessage().' '.' in ' . $this->getFile(). ' on line '. $this->getLine() . ' '; } ?>
9
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(). ' '; }
10
IS 11810 Bob’s Auto Parts - 2 class fileWriteException extends Exception { function __toString() { return 'fileWriteException '. $this->getCode(). ': '. $this->getMessage().' '.' in '. $this->getFile(). ' on line '. $this->getLine(). ' '; }
11
IS 11811 Bob’s Auto Parts - 3 class fileLockException extends Exception { function __toString() { return 'fileLockException '. $this->getCode(). ': '. $this->getMessage().' '.' in '. $this->getFile(). ' on line '. $this->getLine(). ' '; } ?>
12
IS 11812 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 ' Order written. '; }
13
IS 11813 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. '; }
14
IS 11814 Other Error Handling Chapter 25 discusses DEBUGGING This is outside the scope of this course BUT, it would be good to look at it
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.