From ba242ccf1e545ee6a6cb24e2d01c094559be5d55 Mon Sep 17 00:00:00 2001 From: Richard <9120292+Reisclef@users.noreply.github.com> Date: Sat, 24 Nov 2018 02:01:08 +0000 Subject: [PATCH] Short description and code example of Exceptions (#23406) * Short description and code example of Exceptions * Fixed typos and formatting --- guide/english/php/errors/exceptions/index.md | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/guide/english/php/errors/exceptions/index.md b/guide/english/php/errors/exceptions/index.md index f514b40b95..1fc6cefff4 100644 --- a/guide/english/php/errors/exceptions/index.md +++ b/guide/english/php/errors/exceptions/index.md @@ -3,11 +3,20 @@ title: Error Exceptions --- ## Error Exceptions -This is a stub. Help our community expand it. +Similar to other programming languages, you generally want to throw Exceptions when some sort of error occurs. Consider the following example of a `withdraw()` function in a theoretical `BankAccount` class where the balance goes below 0: -This quick style guide will help ensure your pull request gets accepted. +```php +function withdraw($amount) { + $newBalance = $this->balance - $amount; + if ($newBalance < 0) { + throw new Exception('Balance would go below zero'); + } + return $newBalance; +} +``` - +In this case, if the value of ```$this->balance``` was 5 and ```$amount``` was 10, you wouldn't want to authorize the withdrawal. By throwing an Exception, you ensure that the withdrawal doesn't take place if there is not enough money in the account. -#### More Information: - +#### More Information + +- [PHP Manual: Exceptions](http://php.net/manual/en/language.exceptions.php)