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.
EXCEPTION-DRIVEN
DEVELOPMENT
ADAM EL-SODANEY
AUGUST 2016 @ SYMFONY MEETUP LONDON
Exception
Throwable
Error
http://php.net/manual/en/class.throwable.php
Exception
LogicException RuntimeException
AmbiguousOptionsException
DivideByZeroException
ExpiredException
BadConfiguratio...
Exception
LogicException RuntimeException
AmbiguousOptionsException
DivideByZeroException
ExpiredException
BadConfiguratio...
WHAT IS A
LOGIC
EXCEPTION ?
php.net/manual/en/class.logicexception.php
[An] Exception that
represents [an] error
in the pr...
WHAT IS A
LOGIC
EXCEPTION ?
WE
FUCKED
UP!
WHAT IS A
RUNTIME
EXCEPTION ?
YOU
FUCKED
UP!
TEXT
TEXT
LET’S CREATE A CALCULATOR
323,013,394,842
divide by
0
LET’S CREATE A BILL CALCULATION APP
€ 323,013,394,842
between
nobody
PREVENT A
LOGIC
EXCEPTION …
Cannot divide
by zero.
Somebody owes
323 billion Euros
(ie. Greece).
AND MAKE IT A
RUNTIME
EXC...
class MissingInvoiceeException extends RuntimeException

{



}

class DivisionByZeroException extends LogicException

{

...
class Calculator
{
public function divide($x, $y)

{

if (0 === $y) {

throw new DivisionByZeroException(
'Cannot divide b...
try {

return $this->calculator->divide(100, 0);

} catch (DivisionByZeroException $e) {

return INF;
}
HIGH-LEVEL API WIL...
class Till
{

public function splitBill(Bill $bill, array $customers)

{


try {

$this->calculator->divide($bill->total, ...
throw new Exception(
string $message,
int $code = 0,
Exception $previous = null
);
EXCEPTIONS TAKE 3 ARGUMENTS
http://php....
ARGUMENT 1
string $message
* Explain why the exception was thrown.
* Never display the message to the end-user.
* This is ...
SYMFONY EXCEPTIONS TEND TO MAKE
SUGGESTIONS ON HOW TO FIX THEM.
https://github.com/symfony/symfony/blob/2.8/src/Symfony/Co...
OR BETTER YET…
https://twitter.com/DivineOmega/status/696806187526983680
ARGUMENT 3
Exception $previous
* When one action cannot be performed because another
failed.
* When a high-level API cause...
try {
try {
try {
charge_bill();
} catch (DivideByZeroException $e) {
throw new NoInvoiceeException(‘’, 5, $e);
}
} catch ...
A SEQUENCE OF FAILURES CAN BE
AUDITED.
DivideByZeroException
NoInvoiceeException
DebtorNotFoundException
charge_bill()
wri...
ARGUMENT 2
int $code
* Categorize your exceptions…
* …Or identify them.
* Conditions in which the exception occurred can b...
ITUNES ERROR CODES
1015
1601
OYSTER CARD ERROR CODES
new BadDataException(
‘Oyster card cannot be read’,
$code = 1);
new DoubleEntryException(
‘Card already passed through the...
IATA DELAY CODES
> IATA delay codes were created to standardise the reporting
by airlines of commercial flight departure de...
IATA DELAY CODES STARTING WITH…
0 — internal issues
1 — passenger/baggage
2 — cargo/mail
3 — handling
4 — technical
5 — da...
Delay Codes starting with 2 (cargo/mail)
These Codes are used to describe delays caused by Cargo (21-26) and Mail Handling...
class AircraftCleaningDelayException
extends HandlingException
{
$this->code = 35;
}
class HandlingException extends Delay...
DOCTRINE EXCEPTIONS “THROW”
THEMSELVES
https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/ORMException.php
Exception
LogicException RuntimeException
OutOfBoundsException
OutOfRangeException
OverflowException
BadFunctionCallExcept...
LOVE EXCEPTIONS? WHY NOT GO ALL
THE WAY…
https://github.com/Enrise/Frisbee
TEXT
FRISBEE IN ACTION
https://enrise.com/2016/04/enrise-launches-frisbee/
try {
throw new OutOfTimeException(
‘Thanks for listening’
);
} catch (OutOfTimeException $e) {
// Adam Elsodaney
// Senio...
Exception-Driven Development [Lightning talk]
Upcoming SlideShare
Loading in …5
×

of

Exception-Driven Development [Lightning talk] Slide 1 Exception-Driven Development [Lightning talk] Slide 2 Exception-Driven Development [Lightning talk] Slide 3 Exception-Driven Development [Lightning talk] Slide 4 Exception-Driven Development [Lightning talk] Slide 5 Exception-Driven Development [Lightning talk] Slide 6 Exception-Driven Development [Lightning talk] Slide 7 Exception-Driven Development [Lightning talk] Slide 8 Exception-Driven Development [Lightning talk] Slide 9 Exception-Driven Development [Lightning talk] Slide 10 Exception-Driven Development [Lightning talk] Slide 11 Exception-Driven Development [Lightning talk] Slide 12 Exception-Driven Development [Lightning talk] Slide 13 Exception-Driven Development [Lightning talk] Slide 14 Exception-Driven Development [Lightning talk] Slide 15 Exception-Driven Development [Lightning talk] Slide 16 Exception-Driven Development [Lightning talk] Slide 17 Exception-Driven Development [Lightning talk] Slide 18 Exception-Driven Development [Lightning talk] Slide 19 Exception-Driven Development [Lightning talk] Slide 20 Exception-Driven Development [Lightning talk] Slide 21 Exception-Driven Development [Lightning talk] Slide 22 Exception-Driven Development [Lightning talk] Slide 23 Exception-Driven Development [Lightning talk] Slide 24 Exception-Driven Development [Lightning talk] Slide 25 Exception-Driven Development [Lightning talk] Slide 26 Exception-Driven Development [Lightning talk] Slide 27 Exception-Driven Development [Lightning talk] Slide 28 Exception-Driven Development [Lightning talk] Slide 29 Exception-Driven Development [Lightning talk] Slide 30 Exception-Driven Development [Lightning talk] Slide 31 Exception-Driven Development [Lightning talk] Slide 32 Exception-Driven Development [Lightning talk] Slide 33 Exception-Driven Development [Lightning talk] Slide 34 Exception-Driven Development [Lightning talk] Slide 35 Exception-Driven Development [Lightning talk] Slide 36 Exception-Driven Development [Lightning talk] Slide 37
Upcoming SlideShare
Railway Oriented Programming
Next
Download to read offline and view in fullscreen.

0 Likes

Share

Download to read offline

Exception-Driven Development [Lightning talk]

Download to read offline

Exceptions are simple yet powerful. We can stop dead some rogue code in its tracks before it wreaks havoc. Yet they are never quite utilised to their full potential.

The talk we will quickly go through the exception types found in PHP, what they represent, and how you should use them, with examples.

It will also cover how best to create a strategy of identifying the causes of exceptions in your application to quickly debug and minimise repeat offences.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Exception-Driven Development [Lightning talk]

  1. 1. EXCEPTION-DRIVEN DEVELOPMENT ADAM EL-SODANEY AUGUST 2016 @ SYMFONY MEETUP LONDON
  2. 2. Exception Throwable Error http://php.net/manual/en/class.throwable.php
  3. 3. Exception LogicException RuntimeException AmbiguousOptionsException DivideByZeroException ExpiredException BadConfigurationException RecursiveException CorruptDataException DerpException Over9000Exception
  4. 4. Exception LogicException RuntimeException AmbiguousOptionsException DivideByZeroException ExpiredException BadConfigurationException RecursiveException CorruptDataException DerpException Over9000Exception http://knowyourmeme.com/memes/derp http://knowyourmeme.com/memes/its-over-9000
  5. 5. WHAT IS A LOGIC EXCEPTION ? php.net/manual/en/class.logicexception.php [An] Exception that represents [an] error in the program logic. This kind of exception should lead directly to a fix in your code. WHAT IS A RUNTIME EXCEPTION ? [An] Exception thrown if an error which can only be found on runtime occurs. http://php.net/manual/en/class.runtimeexception.php
  6. 6. WHAT IS A LOGIC EXCEPTION ? WE FUCKED UP! WHAT IS A RUNTIME EXCEPTION ? YOU FUCKED UP!
  7. 7. TEXT
  8. 8. TEXT
  9. 9. LET’S CREATE A CALCULATOR 323,013,394,842 divide by 0
  10. 10. LET’S CREATE A BILL CALCULATION APP € 323,013,394,842 between nobody
  11. 11. PREVENT A LOGIC EXCEPTION … Cannot divide by zero. Somebody owes 323 billion Euros (ie. Greece). AND MAKE IT A RUNTIME EXCEPTION . http://www.bbc.co.uk/news/world-europe-33407742
  12. 12. class MissingInvoiceeException extends RuntimeException
 {
 
 }
 class DivisionByZeroException extends LogicException
 {
 
 } …WILL BECOME…
  13. 13. class Calculator { public function divide($x, $y)
 {
 if (0 === $y) {
 throw new DivisionByZeroException( 'Cannot divide by zero.’ );
 }
 
 return $x / $y;
 } } LOW-LEVEL API WILL THROW LOGIC EXCEPTION
  14. 14. try {
 return $this->calculator->divide(100, 0);
 } catch (DivisionByZeroException $e) {
 return INF; } HIGH-LEVEL API WILL CATCH LOGIC EXCEPTION… http://math.stackexchange.com/questions/127376/ https://en.wikipedia.org/wiki/Division_by_zero#/media/File:Hyperbola_one_over_x.svg
  15. 15. class Till {
 public function splitBill(Bill $bill, array $customers)
 { 
 try {
 $this->calculator->divide($bill->total, count($customers));
 } catch (DivisionByZeroException $e) {
 throw new MissingInvoiceeException( 'No-one is currently billed for the amount due.’, 5, $e );
 } }
 } … OR THROW A RUNTIME EXCEPTION
  16. 16. throw new Exception( string $message, int $code = 0, Exception $previous = null ); EXCEPTIONS TAKE 3 ARGUMENTS http://php.net/manual/en/class.exception.php
  17. 17. ARGUMENT 1 string $message * Explain why the exception was thrown. * Never display the message to the end-user. * This is so they can be translated. * It may also contain sensitive information. * Suggest a fix for the exception.
  18. 18. SYMFONY EXCEPTIONS TEND TO MAKE SUGGESTIONS ON HOW TO FIX THEM. https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/DependencyInjection/Compiler/CheckReferenceValidityPass.php#L146 https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/DependencyInjection/Exception/ScopeCrossingInjectionException.php
  19. 19. OR BETTER YET… https://twitter.com/DivineOmega/status/696806187526983680
  20. 20. ARGUMENT 3 Exception $previous * When one action cannot be performed because another failed. * When a high-level API causes errors in a low-level API. * Mistakes occur as the result of a sequence of failures, which can be easily traced.
  21. 21. try { try { try { charge_bill(); } catch (DivideByZeroException $e) { throw new NoInvoiceeException(‘’, 5, $e); } } catch (NoInvoiceeException $f) { if ($debtor = find_debtor()) { chase_debtor($debtor); } throw new DebtorNotFoundException(‘’, 6, $f); } } catch (DebtorNotFoundException $g) { 
 write_off_debt(); }
  22. 22. A SEQUENCE OF FAILURES CAN BE AUDITED. DivideByZeroException NoInvoiceeException DebtorNotFoundException charge_bill() write_off_debt()
  23. 23. ARGUMENT 2 int $code * Categorize your exceptions… * …Or identify them. * Conditions in which the exception occurred can be quickly determined. * Logging of exceptions is better handled.
  24. 24. ITUNES ERROR CODES 1015 1601
  25. 25. OYSTER CARD ERROR CODES
  26. 26. new BadDataException( ‘Oyster card cannot be read’, $code = 1); new DoubleEntryException( ‘Card already passed through the gates’, $code = 21); new InsufficientBalanceException( ‘No valid season ticket or not enough funds.’, $code = 36); IF OYSTER CARDS WERE MANAGED BY PHP… https://www.whatdotheyknow.com/request/189010/response/471377/attach/4/Gate%20Reject%20Codes %20FOI%20request.pdf
  27. 27. IATA DELAY CODES > IATA delay codes were created to standardise the reporting by airlines of commercial flight departure delays. > Previously, every airline had its own system, which made the sharing and aggregation of flight delay information difficult. > IATA standardised the flight delay reporting format by using codes that attribute cause and responsibility for the delay. https://en.wikipedia.org/wiki/IATA_delay_codes
  28. 28. IATA DELAY CODES STARTING WITH… 0 — internal issues 1 — passenger/baggage 2 — cargo/mail 3 — handling 4 — technical 5 — damage/failure 6 — operation 7 — weather 8 — air traffic control 9 — miscellaneous https://www.eurocontrol.int/sites/default/files/content/documents/official-documents/facts- and-figures/coda-reports/standard-iata-delay-codes-ahm730.pdf
  29. 29. Delay Codes starting with 2 (cargo/mail) These Codes are used to describe delays caused by Cargo (21-26) and Mail Handling (27-29). 21 (CD): Documentation, errors, etc. 22 (CP): Late positioning 23 (CC): Late acceptance 24 (CI): Inadequate packing 25 (CO): Oversales, booking errors 26 (CU): Late preparation in warehouse 27 (CE): Mail Oversales, packing, etc. 28 (CL): Mail Late positioning 29 (CA): Mail Late acceptance Delay Codes starting with 3 (handling) These Codes are used to describe delays caused by aircraft and ramp handling 31 (GD): Aircraft documentation late or inaccurate, weight and balance (Loadsheet), general declaration, passenger manifest, etc. 32 (GL): Loading, Unloading, bulky/special load, cabin load, lack of loading staff 33 (GE): Loading Equipment, lack of or breakdown, e.g. container pallet loader, lack of staff 34 (GS): Servicing Equipment, lack of or breakdown, lack of staff, e.g. steps 35 (GC): Aircraft Cleaning 36 (GF): Fuelling, Defuelling, fuel supplier 37 (GB): Catering, late delivery or loading 38 (GU): ULD, Containers, pallets, lack of or breakdown 39 (GT): Technical equipment, lack of or breakdown, lack of staff, e.g. pushback
  30. 30. class AircraftCleaningDelayException extends HandlingException { $this->code = 35; } class HandlingException extends DelayException {} class DelayException extends Exception {} IF IATA DELAYS WERE EXCEPTIONS AND WHERE MANAGED BY PHP…
  31. 31. DOCTRINE EXCEPTIONS “THROW” THEMSELVES https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/ORMException.php
  32. 32. Exception LogicException RuntimeException OutOfBoundsException OutOfRangeException OverflowException BadFunctionCallException BadMethodCallException DomainException InvalidArgumentException LengthException RangeException UnderflowException UnexpectedValueException http://php.net/manual/en/spl.exceptions.php
  33. 33. LOVE EXCEPTIONS? WHY NOT GO ALL THE WAY… https://github.com/Enrise/Frisbee
  34. 34. TEXT
  35. 35. FRISBEE IN ACTION https://enrise.com/2016/04/enrise-launches-frisbee/
  36. 36. try { throw new OutOfTimeException( ‘Thanks for listening’ ); } catch (OutOfTimeException $e) { // Adam Elsodaney // Senior Symfony developer at REISS // Follow me on Twitter @ArchFizz } FIN https://github.com/adamelso

Exceptions are simple yet powerful. We can stop dead some rogue code in its tracks before it wreaks havoc. Yet they are never quite utilised to their full potential. The talk we will quickly go through the exception types found in PHP, what they represent, and how you should use them, with examples. It will also cover how best to create a strategy of identifying the causes of exceptions in your application to quickly debug and minimise repeat offences.

Views

Total views

788

On Slideshare

0

From embeds

0

Number of embeds

3

Actions

Downloads

5

Shares

0

Comments

0

Likes

0

×