🔥 CodeIgniter Intermediate

How does CI4 handle error pages and exceptions?

Why Interviewers Ask This

This question targets practical, hands-on experience with CodeIgniter. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

CodeIgniter 4 provides a centralized exception handling system. By default, all unhandled exceptions are caught by CI4's Exceptions handler and displayed as error pages. In production (CI_ENVIRONMENT = production), generic error pages are shown; in development, full stack traces are displayed. Custom error views are placed in app/Views/errors/html/ — create error_404.php, error_500.php, etc. Throw HTTP errors: throw new \CodeIgniter\Exceptions\PageNotFoundException() → 404, or use the shorthand show_404(). Custom exception classes: extend base exception classes and catch them in controllers or configure them in app/Config/Exceptions.php. The log_exception() parameter controls whether each exception type is logged. Override the handler with a custom class by updating app/Config/Exceptions.php.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.