chroma-x / common-exceptions
一个PHP库,提供不同项目使用的常用异常类。
3.0.2
2021-01-18 12:59 UTC
Requires
- php: >=5.3
README
一个PHP库,提供不同项目使用的常用异常类。
安装
{
"require": {
"chroma-x/common-exceptions": "~3.0"
}
}
使用
自动加载和命名空间
require_once('path/to/vendor/autoload.php');
处理异常
通过具体类捕获
try{
// Perform something maybe throwing an exception
} catch (ChromaX\CommonException\NetworkException\ConnectionException $exception) {
// API is not reachable
} catch (ChromaX\CommonException\NetworkException\CurlException $exception) {
// Curl failed
} catch (ChromaX\CommonException\ApiException\InvalidResponseException $exception) {
// API returns an unexpected result
} catch (ChromaX\CommonException\ApiException\RequestQuotaException $exception) {
// API requests over the allowed limit
} catch (ChromaX\CommonException\ApiException\NoResultsException $exception) {
// API request had no result
} catch (ChromaX\CommonException\IoException\FileWriteException $exception) {
// Log file was not writable
}
通过父类捕获
try{
// Perform something maybe throwing an exception
} catch (ChromaX\CommonException\NetworkException\Base\NetworkException $exception) {
// Any network exception was thrown
} catch (ChromaX\CommonException\ApiException\Base\ApiException $exception) {
// Any API exception was thrown
} catch (ChromaX\CommonException\IoException\Base\IoException $exception) {
// Any IO exception was thrown
}
通过祖父类捕获
try{
// Perform something maybe throwing an exception
} catch (ChromaX\CommonException\Base\BaseException $exception) {
// Any exception was thrown
}
贡献
对我们项目的贡献总是非常受欣赏。
但是:请遵循CONTRIBUTING.md文档中写下的贡献指南。
许可证
PHP Common Exceptions遵循MIT许可证。