|
| 1 | +--- |
| 2 | +isChild: true |
| 3 | +--- |
| 4 | + |
| 5 | +## Exceptions |
| 6 | + |
| 7 | +Exceptions are a standard part of most popular programming languages, but they are often overlooked by PHP programmers. |
| 8 | +Languages like Ruby are extremely Exception heavy, so whenever something goes wrong such as a HTTP request failing, or |
| 9 | +a DB query goes wrong, or even if an image asset could not be found, Ruby (or the gems being used) will throw an |
| 10 | +exception to the screen meaning you instantly know there is a mistake. |
| 11 | + |
| 12 | +PHP itself is fairly lax with this, and a call to `file_get_contents()` will usually just get you a `FALSE` and a warning. |
| 13 | +Many older PHP frameworks like CodeIgniter will just return a false, log a message to their proprietary logs and maybe |
| 14 | +let you use a method like `$this->upload->get_error()` to see what went wrong. The problem here is that you have to go |
| 15 | +looking for a mistake and check the docs to see what the error method is for this class, instead of having it made extremely |
| 16 | +obvious. |
| 17 | + |
| 18 | +Another problem is when classes automatically throw an error to the screen and exit the process. When you do this you |
| 19 | +stop another developer from being able to dynamically handle that error. Exceptions should be throw to make a developer aware |
| 20 | +of an error, then they can choose how to handle this. E.g: |
| 21 | + |
| 22 | +{% highlight php %} |
| 23 | +<?php |
| 24 | +$email = new Fuel\Email; |
| 25 | +$email->subject('My Subject'); |
| 26 | +$email->body('How the heck are you?'); |
| 27 | +$email->to(' [email protected]', 'Some Guy'); |
| 28 | + |
| 29 | +try |
| 30 | +{ |
| 31 | + $email->send(); |
| 32 | +} |
| 33 | +catch(Fuel\Email\ValidationFailedException $e) |
| 34 | +{ |
| 35 | + // The validation failed |
| 36 | +} |
| 37 | +catch(Fuel\Email\SendingFailedException $e) |
| 38 | +{ |
| 39 | + // The driver could not send the email |
| 40 | +} |
| 41 | +{% endhighlight %} |
| 42 | + |
| 43 | +### SPL Exceptions |
| 44 | + |
| 45 | +An Exception by default has no meaning and the most common to give it meaning is by setting its name: |
| 46 | + |
| 47 | +{% highlight php %} |
| 48 | +<?php |
| 49 | +class ValidationException extends Exception {} |
| 50 | +{% endhighlight %} |
| 51 | +
|
| 52 | +This means you can add multiple catch blocks and handle different Exceptions differently. This can lead to |
| 53 | +the creation of a <em>lot</em> of custom Exceptions, some of which could have been avoided using the SPL Exceptions |
| 54 | +provided in the [SPL extension][splext]. |
| 55 | + |
| 56 | +If for example you use the `__call()` Magic Method and an invalid method is requested then instead of throwing a standard |
| 57 | +Exception which is vague, or creating a custom Exception just for that, you could just `throw new BadFunctionCallException;`. |
| 58 | + |
| 59 | +* [Read about Exceptions][exceptions] |
| 60 | +* [Read about SPL Exceptions][splexe] |
| 61 | + |
| 62 | +[exceptions]: http://php.net/manual/en/language.exceptions.php |
| 63 | +[splexe]: http://php.net/manual/en/spl.exceptions.php |
| 64 | +[splext]: /#standard_php_library |
0 commit comments