static function catchErrorsOn ($wrappedCode, $errorHandler = null, $reset = true)
  {
    if (PHP_MAJOR_VERSION >= 7) {
      try {
        // Run the caller-supplied code.
        return $wrappedCode();
      }
      catch (Throwable $e) {
        // Handle the error.
        if (isset($errorHandler))
          return $errorHandler($e);

        throw $e;
      }
    }

    $prevHandler = set_error_handler (function ($errno, $errstr, $errfile, $errline) {
      if (!error_reporting ())
        return false;
      throw new ErrorException ($errstr, $errno, 0, $errfile, $errline);
    });

    try {
      // Run the caller-supplied code.
      $r = $wrappedCode();
      // Restore the previous error handler.
      set_error_handler ($prevHandler);

      return $r;
    }
    catch (Exception $e) {
      // Intercept the error that will be triggered below.
      set_error_handler (function () {
        // Force error_get_last() to be set.
        return false;
      });
      // Clear the current error message so that the framework will not intercept the previous error.
      if ($reset)
        trigger_error ("");
      // Restore the previous error handler.
      set_error_handler ($prevHandler);

      // Handle the error.
      if (isset($errorHandler))
        return $errorHandler($e);

      throw $e;
    }
  }