|
1 |
| -# php-exceptions |
| 1 | +# The helpful custom PHP exceptions |
| 2 | + |
| 3 | +[](https://packagist.org/packages/bsadnu/helpers-box) |
| 4 | +[](https://packagist.org/packages/bsadnu/helpers-box) |
| 5 | +[](https://packagist.org/packages/bsadnu/helpers-box) |
| 6 | + |
| 7 | +This lets you catch different-layers-specific exceptions (if needed). |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +The best way to use this component is through the Composer: |
| 12 | + |
| 13 | +```BASH |
| 14 | +composer require bsadnu/php-exceptions |
| 15 | +``` |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +- You may use exceptions within the standard throwing/catching mechanism or even extend them in any way you want: |
| 20 | + |
| 21 | +```php |
| 22 | +use Bsadnu\Exception\InvalidArgumentException; |
| 23 | + |
| 24 | +... |
| 25 | + |
| 26 | +try { |
| 27 | + ... |
| 28 | +} catch (InvalidArgumentException $exception) { |
| 29 | + ... |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +```php |
| 34 | +use Bsadnu\Exception\TypeError; |
| 35 | + |
| 36 | +... |
| 37 | + |
| 38 | +throw new TypeError($message); |
| 39 | +``` |
| 40 | + |
| 41 | +- It is really helpful to catch the base `ExceptionInterface` sometimes. Therefore, you can control a group of exceptions. Moreover, you may add manually as many specific interfaces as you wish. |
| 42 | + |
| 43 | +## More detailed description |
| 44 | + |
| 45 | +All exceptions used in this library implements `\Bsadnu\Exception\ExceptionInterface`, and extend one of the SPL extensions. This way, you have several ways of catching specific extension groups: |
| 46 | +1. Catch all exceptions: `\Exception` |
| 47 | +2. Catch all exceptions thrown by the library: |
| 48 | +`\Bsadnu\Exception\ExceptionInterface` |
| 49 | +3. Catch a specific SPL exception: `\LogicException` |
| 50 | +4. Catch a specific SPL exception thrown by the library: |
| 51 | +`\Bsadnu\Exception\LogicException` |
| 52 | + |
| 53 | +The base `\Bsadnu\Exception\ExceptionInterface` consists of `getUnit/setUnit` methods which can be used to describe the place of code (Unit) where an exception occurs. |
| 54 | + |
| 55 | +## Contributing |
| 56 | + |
| 57 | +All feedback / bug reports / pull requests are welcome. |
| 58 | + |
| 59 | +## License |
| 60 | + |
| 61 | +This code is licensed under the [MIT License](LICENSE). |
0 commit comments