Back to course

Error Handling and Reporting (E_NOTICE, E_WARNING)

PHP: The Complete 0 to Hero Bootcamp

41. Error Handling and Reporting

Proper error handling is vital for security and maintainability. PHP errors are categorized by severity.

Types of Errors

  1. Notices (E_NOTICE): Runtime issues that aren't critical (e.g., accessing an undefined variable). PHP keeps running.
  2. Warnings (E_WARNING): More serious runtime problems (e.g., including a missing file via include). PHP keeps running.
  3. Fatal Errors (E_ERROR): Critical problems (e.g., calling an undefined function, using require on a missing file). Stops script execution immediately.

Controlling Error Reporting

The error_reporting() function controls which errors PHP displays. It is crucial to hide errors on production servers.

php

Try-Catch Blocks (Exception Handling)

For OOP and modern applications, we handle errors using throw (to raise an error/exception) and try...catch (to gracefully manage it).

php

Caught Exception: " . $e->getMessage(); // Typically you would log this error, not just echo it } finally { // This block runs regardless of success or failure echo "\nFinished attempting division."; } ?>