freeCodeCamp/guide/english/php/errors/exceptions/index.md

23 lines
848 B
Markdown
Raw Normal View History

2018-10-12 19:37:13 +00:00
---
title: Error Exceptions
---
## Error Exceptions
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:
2018-10-12 19:37:13 +00:00
```php
function withdraw($amount) {
$newBalance = $this->balance - $amount;
if ($newBalance < 0) {
throw new Exception('Balance would go below zero');
}
return $newBalance;
}
```
2018-10-12 19:37:13 +00:00
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.
2018-10-12 19:37:13 +00:00
#### More Information
- [PHP Manual: Exceptions](http://php.net/manual/en/language.exceptions.php)