Catch All Errors & Exceptions In PHP (Simple Examples)

Welcome to a quick tutorial on how to catch all errors and exceptions in PHP. Need to cast a net and capture all the errors to figure out what went wrong?

We can capture all errors in PHP using the set_exception_handler() function. For example:

  • function eecho ($ex) { echo $ex->getMessage(); }
  • set_exception_handler("eecho");

That covers the quick basics, but read on if you need detailed examples.

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

PHP CATCH ALL ERRORS

All right, let us now get into examples of catching all errors and exceptions in PHP.

 

1) BASIC PHP ERROR HANDLING

1-basic-error.php
<?php
// (A) ERROR REPORTING LEVEL
// E_ALL, E_WARNING, E_STRICT, E_PARSE, E_DEPRECATED, E_NOTICE
error_reporting(E_ALL);
 
// (B) SAVE TO ERROR LOG FILE
ini_set("log_errors", 1);
ini_set("error_log", __DIR__ . DIRECTORY_SEPARATOR . "error.log");
 
// (C) DISPLAY ERROR MESSAGES?
// NOTE: WILL SHOW "GENERIC" 500 ERROR IF TURNED OFF & NO SET_EXCEPTION_HANDLER()
// NOTE: IF ERROR_REPORTING(0), THIS WILL NOT SHOW ANYTHING.
ini_set("display_errors", 1);
 
// (D) THROW EXCEPTION
throw new Exception("TEST ERROR!");

Before we get into “catch errors”, here is a quick crash course on the default PHP error handling mechanisms. Yes, this is important. Some people skipped the basics, and wonder why “errors are not showing” when error reporting is turned off…

  1. Use error_reporting() to control what kinds of errors need to be reported – From everything E_ALL, to warnings E_WARNING, to boring notices E_NOTICE. I will leave a link to the full list below.
  2. log_errors and error_log to define if errors should be saved to a log file.
  3. Lastly, use display_errors to control if errors should be displayed on the screen during runtime.
    • Not to be confused – We can turn off display_errors, but keep errors in a log file.
    • But when error_reporting() is set to “don’t report any errors”, PHP will not show any errors nor save them into the log file.

P.S. All of these can be set in php.ini.

 

 

2) DISPLAYING A CUSTOM ERROR SCREEN

2-custom-error.php
<?php
// (A) ERROR HANDLING SETTINGS
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 0);
ini_set("log_errors", 1);
ini_set("error_log", __DIR__ . DIRECTORY_SEPARATOR . "error.log");
 
// (B) CATCH ALL ERRORS
set_exception_handler(function($ex) {
  // (B1) DO YOUR OWN ERROR HANDLING IF YOU WANT
  // print_r($ex);
  // error_log("YOUR EXTRA MESSAGE OR DATA");
 
  // (B2) SHOW CUSTOM ERROR MESSAGE ?>
  <div style="position:fixed; top:0; left:0; z-index:99999;
       width:100vw; height:100vw; padding:10px; background:#fff">
    <h1>HAIYAA, AN ERROR HAS OCCURED.</h1>
    <p>The developers have been shamed and disowned from the family.</p>
  </div>
<?php });
 
// (C) THROW EXCEPTION
throw new Exception("TEST ERROR!");
echo "This will not run";

As in the introduction above, we use the set_exception_handler() function to capture all the errors and exceptions. In what situation should we do this? If you need to do a “graceful crash”.

  • Save some data, so the user doesn’t lose everything. Or maybe even a possible “recovery” option.
  • Show your custom error message.

 

 

3) SAVE ERRORS TO THE DATABASE

3A) ERROR DATABASE TABLE

3a-error.sql
CREATE TABLE `errors` (
  `date` datetime NOT NULL DEFAULT current_timestamp(),
  `file` varchar(255) NOT NULL,
  `line` varchar(255) NOT NULL,
  `message` text NOT NULL,
  `trace` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
 
ALTER TABLE `errors`
  ADD KEY `date` (`date`);

If you want to save the error into the database, here’s a quick dummy table you can work on.

 

3B) ERROR TO DATABASE

3b-db-error.php
<?php
// (A) DATABASE SETTINGS - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8");
define("DB_USER", "root");
define("DB_PASSWORD", "");
 
// (B) ERROR HANDLING SETTINGS
error_reporting(E_ALL & ~E_NOTICE);
ini_set("display_errors", 0); 
ini_set("log_errors", 0);
 
// (C) ERROR HANDLER
set_exception_handler(function($ex) {
  // (C1) CONNECT TO DATABASE
  $pdo = new PDO(
    "mysql:host=". DB_HOST .";charset=". DB_CHARSET .";dbname=". DB_NAME,
    DB_USER, DB_PASSWORD, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
  ]);
 
  // (C2) SAVE ERROR TO DATABASE
  $stmt = $pdo->prepare("INSERT INTO `errors` (`file`, `line`, `message`, `trace`) VALUES (?,?,?,?)");
  $stmt->execute([
    $ex->getFile(), $ex->getLine(),
    $ex->getMessage(), $ex->getTraceAsString()
  ]);
 
  // (C3) THEN SHOW YOUR CUSTOM ERROR MESSAGE
  echo "Mr. Stark, I don't feel so good.";
});
 
// (D) THROW EXCEPTION
throw new Exception("TEST ERROR!");
echo "This will not run";

This should be pretty self-explanatory – We save any errors into the database instead.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Catch All Errors In PHP (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

2 thoughts on “Catch All Errors & Exceptions In PHP (Simple Examples)”

    1. Thanks for sharing. Sorry if this is rude, but that snippet makes very little sense.

      1) Read the tutorial carefully, it is about using set_exception_handler() to capture all errors and handle them gracefully.
      2) The error message can be obtained from the exception itself – $ex->getMessage(). Also the file $ex->getFile(), line $ex->getLine(), and stack trace $ex->getTraceAsString().
      3) Notice how I deliberately left out the PHP error code $ex->getCode()? That’s how little value it provided for debugging in years of my web development career. If you need it somehow – https://www.php.net/manual/en/errorfunc.constants.php
      4) BAD idea to display the full error message, code, script name, and line number on a live system. Huge security risk.
      5) Just show a simple “an error has occurred” to the user where possible. Or just the error message without file name, line, trace, and error code.
      6) Keep error details internal – In a log file, or automatically send an email on critical stops.
      7) What’s the point of glorifying the error details in a popup box? Are jQuery and Bootstrap really necessary? If you want to show an error on the screen, just do a <?=$ex->getMessage()?>.

      P.S. The error code is not totally useless. It can still be used for “graceful failures” such as shutting down on critical errors – if (CODE == 4096) { SHUT DOWN }.

Leave a Comment

Your email address will not be published. Required fields are marked *