Your SlideShare is downloading. ×
0
Jan Tvrdík
try {    ...    if (!$ok) {        throw new FooException();    }} catch (FooException $e) {     ...}
try {    ...    if (!$ok) {        throw new FooException();    }} catch (FooException $e) {     ...} catch (BarException ...
try {    ...    if (!$ok) {        throw new FooException();    }} catch (FooException $e) {     ...} finally {     ...}
try                                 No             Was there       an exception thrown?Yes      Is there a corresponding  ...
try {    $this->db->lock(users);    $this->db->query(...);    $this->db->unlock();// intentionally catch all exceptions} c...
try {    $this->db->lock(users);    $this->db->query(...);    $this->db->unlock();// intentionally catch all exceptions} c...
try {    $this->db->lock(users);    $this->db->query(...);} finally {    $this->db->unlock();}
try {    return 1;} finally {    return 2;}
constructor parameters  string    $message = ""  int       $code     = 0  Exception $previous = NULLmethods  string      g...
usage errorruntime error  System failure
can be avoidedshould directly lead to a fixmessage is more important than typevery important in librariesshould not be cat...
can NOT be avoidedtype is more important than messageshould be catchedoften related to thread safetyexamples:  file / dire...
ExceptionErrorExceptionLogicException (for usage errors)   InvalidArgumentExceptionRuntimeException (for runtime errors)
Exception  LogicException    AppLogicException      AppInvalidArgumentException      AppNotImplementedException      AppSt...
Exception  LogicException    …  RuntimeException    AppRuntimeException      AppIOException        AppFileNotFoundExceptio...
throw only exceptions from your namespaceusage error  write nice ex. message for human developerruntime error  use very sp...
never display ex. message to the usercatch as specific types as possibleread and respect @throw annotationsuse $previous w...
Twitter + GitHub  @JanTvrdik                   nette.merxes.cz
Exceptions in PHP
Upcoming SlideShare
Loading in...5
×

Exceptions in PHP

836

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
836
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×