📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials CodeIgniter 4 CI4 Custom Exceptions

CI4 Custom Exceptions

5 min read
Create domain exception classes and a global exception handler for clean API error responses.

Custom Exceptions

namespace App\Exceptions;

class NotFoundException extends \RuntimeException {
    public static function forPost(int $id): static {
        return new static("Post #" . $id . " not found", 404);
    }
}

// Throw
throw NotFoundException::forPost($id);