{"id":19913671,"url":"https://github.com/jason-napolitano/php-exceptions","last_synced_at":"2026-06-04T21:31:05.261Z","repository":{"id":165271809,"uuid":"198152480","full_name":"jason-napolitano/PHP-Exceptions","owner":"jason-napolitano","description":"A collection of practical exception classes for more verbose PHP exception handling","archived":false,"fork":false,"pushed_at":"2022-01-21T23:02:59.000Z","size":896,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-11T23:12:43.153Z","etag":null,"topics":["exceptions","oop","php","php7"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/jason-napolitano/php-exceptions","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jason-napolitano.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-22T05:20:53.000Z","updated_at":"2023-03-24T16:22:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"a9e89095-2771-453b-b7c9-ca86f0d070f3","html_url":"https://github.com/jason-napolitano/PHP-Exceptions","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason-napolitano%2FPHP-Exceptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason-napolitano%2FPHP-Exceptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason-napolitano%2FPHP-Exceptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jason-napolitano%2FPHP-Exceptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jason-napolitano","download_url":"https://codeload.github.com/jason-napolitano/PHP-Exceptions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241342042,"owners_count":19947135,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exceptions","oop","php","php7"],"created_at":"2024-11-12T21:33:41.650Z","updated_at":"2025-03-01T09:17:22.302Z","avatar_url":"https://github.com/jason-napolitano.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Exceptions\n\n[![Latest Stable Version](https://poser.pugx.org/jason-napolitano/php-exceptions/version)](https://packagist.org/packages/jason-napolitano/php-exceptions)\n[![Total Downloads](https://poser.pugx.org/jason-napolitano/php-exceptions/downloads)](https://packagist.org/packages/jason-napolitano/php-exceptions)\n[![License](https://poser.pugx.org/jason-napolitano/php-exceptions/license)](https://packagist.org/packages/jason-napolitano/php-exceptions)\n[![Build Status](https://travis-ci.com/jason-napolitano/PHP-Exceptions.svg?branch=master)](https://travis-ci.com/jason-napolitano/PHP-Exceptions)\n[![Coverage Status](https://coveralls.io/repos/github/jason-napolitano/PHP-Exceptions/badge.svg?branch=master)](https://coveralls.io/github/jason-napolitano/PHP-Exceptions?branch=development)\n\n## Update:\n**This repository has been archived and for the time being is considered abandonware. Please use at your own discretion.**\n\n## Synopsis\n`PHP Exceptions` is an attempt to encapsulate a larger variation of exception classes that do not exist \nwithin PHP's SPL, and separate them in an orchestrated fashion by containing them within their own \n`\\Exceptions` namespace.\n\nAs said by [Arnout Boks](https://www.moxio.com/blog/34/best-practices-for-php-exception-handling):\n\n\"_Proper exception handling takes quite some effort, but will eventually result in a much more stable \napplication._ **A sensible exception handling strategy makes it clear what exceptions should be expected \n(and thus handled!) at a given point in the code.** _Moreover it will maintain the encapsulation and \nabstraction you carefully applied to your object-oriented design. Last but not least, it should make \ndebugging a breeze._\"\n\nProper Exception handling is a **must** in PHP applications, and application development in general. \nThis library hopes to encourage developers to properly handle exceptions, by giving them new and more \ntailored exception classes to work with. This package adds a new variety and flavor when\nbuilding applications. For example, there are now exceptions for:\n\nThe File System\n```php\nthrow new \\Exceptions\\FileSystem\\FileNotFoundException();\n```\n```php\nthrow new \\Exceptions\\FileSystem\\DirectoryAlreadyExistsException();\n```\n\nArrays, objects \u0026 JSON\n```php\nthrow new \\Exceptions\\Collection\\ArrayNotExistsException();\n```\n```php\nthrow new \\Exceptions\\Collection\\ObjectAlreadyExistsException();\n```\n```php\nthrow new \\Exceptions\\Collection\\InvalidJSONException();\n```\n\nArithmetic\n```php\nthrow new \\Exceptions\\Operation\\DivideByNegativeNumberException();\n```\n\nRevised PHP SPL's\n```php\nthrow new \\Exceptions\\JsonException();\n```\n```php\nthrow new \\Exceptions\\RuntimeException();\n```\n```php\nthrow new \\Exceptions\\DomainException();\n```\n\nAnd quite a few more with new ones being implemented all the time. All tested with PHPUnit 8+. And take \nnote that these may be treated just like any standard PHP exceptions since they in fact `extend` these same\nstandard PHP exceptions. Meaning that an optional message, code, etc may be passed into the `__construct()`.\nFramework versions will be available shortly after the official v.1.0 release.\n\n## Table of contents\n - [Synopsis](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/README.md#synopsis)\n - [Contributing](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/CONTRIBUTING.md)\n - [Useful Links](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/README.md#useful-links)\n - [Requirements](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/README.md#requirements)\n - [Installation](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/README.md#installation)\n - [License](https://github.com/jason-napolitano/PHP-Exceptions/blob/master/README.md#license)\n \n### Class Blueprint\nTake a look at the following class blueprint ([PHP.net](https://www.php.net/manual/en/class.exception.php)):\n\n```php\nnamespace Exceptions\\NamespaceName;\n    \nclass ExampleException extends \\RuntimeException\n{\n    /* Properties */\n    protected string $message;\n    protected int $code;\n    protected string $file;\n    protected int $line;\n\n    /* Methods */\n    public __construct ([ string $message = \"\" [, int $code = 0 [, Throwable $previous = NULL ]]] )\n    final public getMessage ( void ) : string\n    final public getPrevious ( void ) : Throwable\n    final public getCode ( void ) : mixed\n    final public getFile ( void ) : string\n    final public getLine ( void ) : int\n    final public getTrace ( void ) : array\n    final public getTraceAsString ( void ) : string\n    public __toString ( void ) : string\n    final private __clone ( void ) : void\n}\n\n```\n\n## Useful Links\n - [Package Tests](https://github.com/jason-napolitano/PHP-Exceptions/tree/master/tests)\n - PHP Links:\n   - [PHP Exceptions](https://www.php.net/manual/en/language.exceptions.php)\n   - [PHP SPL](https://www.php.net/manual/en/spl.exceptions.php)\n - PHP FIG:\n   - [PHP-FIG PSR4](https://www.php-fig.org/psr/psr-4/)\n - Composer:\n   - [Get Composer](https://getcomposer.org/)\n   \n ## Requirements\n  - PHP \u003e= 7.4 and [`ext-json`](https://www.php.net/manual/en/book.json.php)\n    - Due to the use of [`\\JsonException`](https://www.php.net/manual/en/class.jsonexception.php) and the use of typed properties [[link]](https://www.php.net/manual/en/migration74.new-features.php).\n  - Composer\n \n ## Installation\n ```php\n composer require jason-napolitano/php-exceptions\n ```\n \n ## Basic Usage\n ```php\n// Require composer's autoloader\nrequire_once = '/path/to/vendor/autoload.php';\n \ntry {\n\t$person = [\n\t\t'name' =\u003e 'My Name',\n\t\t'age'  =\u003e 21,\n\t\t'city' =\u003e null\n\t];\n\tif ( array_key_exists('gender', $person) ) {\n\t    return $person['gender'];\n\t    \n\t} else {\n\t\tthrow new \\Exceptions\\Collection\\ArrayKeyNotExistsException();\n\t}\n} catch (Exception $e) {\n\techo 'Caught Exception: ',  $e-\u003egetMessage(), \"\\n\";\n}\n```\n\n### OOP Usage:\n * _Don't forget your composer `autoload.php` file_\n```php\nclass ClassName\n{\n    use \\Exceptions\\Helpers\\ExceptionHelpers;\n\n    public function methodName()\n    {\n        // Using a namespace\n        self::throwNew('FileNotFoundException', 'FileSystem');\n\n        // Using default namespace\n        self::throwNew('RangeException');\n\n        // Using the default \\Exceptions\\RuntimeException\n        self::throwNew();\n    }\n}\n```\n\n ## License\n ### MIT License\n \n #### Copyright (c) 2019 Jason Napolitano\n \n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n \n The above copyright notice and this permission notice shall be included in all\n copies or substantial portions of the Software.\n \n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjason-napolitano%2Fphp-exceptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjason-napolitano%2Fphp-exceptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjason-napolitano%2Fphp-exceptions/lists"}