This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Jan Tvrdk
try { ... if (!$ok) { throw new FooException(); }} catch
(FooException $e) { ...}
try { ... if (!$ok) { throw new FooException(); }} catch
(FooException $e) { ...} catch (BarException $e) { ...}
try { ... if (!$ok) { throw new FooException(); }} catch
(FooException $e) { ...} finally { ...}
try No Was there an exception thrown?Yes Is there a
corresponding No catch block?Yes catch finally
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
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
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
never display ex. message to the usercatch as specific types as
possibleread and respect @throw annotationsuse $previous when
rethrowing ex.