PHP code example of holicz / simple-exception

1. Go to this page and download the library: Download holicz/simple-exception library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

holicz / simple-exception example snippets




namespace App\Exception;

use holicz\SimpleException\BaseException;
use holicz\SimpleException\ExceptionContext;

class CouldNotRemoveArticleException extends BaseException
{
    public function __construct(int $id)
    {
        $exceptionContext = new ExceptionContext(
            'There was an error during article removal. Please try again later.',
            sprintf('Could not delete article with id %d', $id),
            500 // HTTP status code
        );
    
        parent::__construct($exceptionContext);
    }
}

try {
    ...
} catch (MyException $e) {
    // Available methods
    $e->getPublicMessage(); // Show to user
    $e->getDebugMessage(); // Log
    $e->getStatusCode();
}