Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Exceptions in PHP

1,076 views

Published on

Best practices for using exceptions in PHP.
How to design exception hierarchies.
Difference between runtime and usage (logic) error.

Published in: Technology
  • Be the first to comment

Exceptions in PHP

  1. 1. Jan Tvrdík
  2. 2. try { ... if (!$ok) { throw new FooException(); }} catch (FooException $e) { ...}
  3. 3. try { ... if (!$ok) { throw new FooException(); }} catch (FooException $e) { ...} catch (BarException $e) { ...}
  4. 4. try { ... if (!$ok) { throw new FooException(); }} catch (FooException $e) { ...} finally { ...}
  5. 5. try No Was there an exception thrown?Yes Is there a corresponding No catch block?Yes catch finally
  6. 6. try { $this->db->lock(users); $this->db->query(...); $this->db->unlock();// intentionally catch all exceptions} catch (Exception $e) { $this->db->unlock(); throw $e;}
  7. 7. try { $this->db->lock(users); $this->db->query(...); $this->db->unlock();// intentionally catch all exceptions} catch (Exception $e) { $this->db->unlock(); throw $e;}
  8. 8. try { $this->db->lock(users); $this->db->query(...);} finally { $this->db->unlock();}
  9. 9. try { return 1;} finally { return 2;}
  10. 10. constructor parameters string $message = "" int $code = 0 Exception $previous = NULLmethods string getMessage() Exception getPrevious() mixed getCode() string getFile() int getLine() array getTrace()
  11. 11. usage errorruntime error System failure
  12. 12. can be avoidedshould directly lead to a fixmessage is more important than typevery important in librariesshould not be catchedexamples: wrong argument type / value calling method in incorrect order creating an instance of static class calling not implemented method
  13. 13. can NOT be avoidedtype is more important than messageshould be catchedoften related to thread safetyexamples: file / directory / database entry not found duplicate entry in database
  14. 14. ExceptionErrorExceptionLogicException (for usage errors) InvalidArgumentExceptionRuntimeException (for runtime errors)
  15. 15. Exception LogicException AppLogicException AppInvalidArgumentException AppNotImplementedException AppStaticClassException RuntimeException …
  16. 16. Exception LogicException … RuntimeException AppRuntimeException AppIOException AppFileNotFoundException AppDirectoryNotFoundException AppEntryNotFoundException AppDuplicateEntryException
  17. 17. throw only exceptions from your namespaceusage error write nice ex. message for human developerruntime error use very specific type (often requires creating a new one) message is often uselessphpDoc Always specify all runtime exceptions @throw annotation is part of API
  18. 18. never display ex. message to the usercatch as specific types as possibleread and respect @throw annotationsuse $previous when rethrowing ex.
  19. 19. Twitter + GitHub @JanTvrdik nette.merxes.cz

×