title: PHP Fatal error: Uncaught Error: Class Exception not found
body: <p>I'm getting roadblocks trying to add error handling to PHP. Currently I have a try and <code>try</code> and <code>catch</code> to handle an error when a $db is trying to <code>insert-&gt;</code> write into a DB that is <code>--read-only</code>. I get the following error:</p>
  
  <blockquote>
    <p>PHP Fatal error: Uncaught Error: Class 'StatCollector\Exception' not
    found in wp-content/mu-plugins/stat-collector/StatCollectorFunctions.php</p>
  </blockquote>
  
  <pre><code>&lt;?php
  
  namespace StatCollector;
  
  function drools_request($data, $uid) {
    try {
      $db = _get_db();
      $insertion = $db-&gt;insert("requests", [
        "uid" =&gt; $uid,
        "data" =&gt; json_encode($data),
      ]);
      if( !$insertion ) {
        throw new Exception('Error writing to the database:');
      }
    }
    catch(\Exception $e)
    {
      echo 'Error writing to the database: ',  $e-&gt;getMessage(), "\n";
    }
  }
  </code></pre>
  
  <p>As you see from the above snippet I have a <code>catch(\Exception $e)</code> that should be catching the Exception generated on the <code>throw new Exception</code>. However this is not the case and I get an error on WP Engine, without the application working.</p>
  
  <p>Why is this the try and catch not working in this case?
  Here is the full class:</p>
  
  <pre><code>&lt;?php
  
  namespace StatCollector;
  
  function drools_request($data, $uid) {
    try {
      $db = _get_db();
      $insertion = $db-&gt;insert("requests", [
        "uid" =&gt; $uid,
        "data" =&gt; json_encode($data),
      ]);
      if( !$insertion ) {
        throw new Exception('Error writing to the database:');
      }
    }
    catch(\Exception $e)
    {
      echo 'Error writing to the database: ',  $e-&gt;getMessage(), "\n";
    }
  }
  
  function drools_response($response, $uid) {
    try {
      $db = _get_db();
      $insertion = $db-&gt;insert("responses", [
        "uid" =&gt; $uid,
        "data" =&gt; json_encode($response),
      ]);
      if( !$insertion ) {
        throw new Exception('Error writing to the database:');
      }
    }
    catch(\Exception $e)
    {
      echo 'Error writing to the database: ',  $e-&gt;getMessage(), "\n";
    }
  }
  
  function results_sent($type, $to, $uid, $url = null, $message = null) {
    try {
      $db = _get_db();
      $insertion = $db-&gt;insert("messages", [
        "uid" =&gt; $uid,
        "msg_type" =&gt; strtolower($type),
        "address" =&gt; $to,
        "url" =&gt; $url,
        "message" =&gt; $message
      ]);
      if( $insertion ) {
        throw new Exception('Error writing to the database:');
      }
    }
    catch(\Exception $e)
    {
      echo 'Error writing to the database: ',  $e-&gt;getMessage(), "\n";
    }
  }
  
  function peu_data($staff, $client, $uid) {
    try {
      if (empty($uid)) {
        return;
      }
      $db = _get_db();
  
      if (! empty($staff)) {
        $insertion = $db-&gt;insert("peu_staff", [
          "uid" =&gt; $uid,
          "data" =&gt; json_encode($staff)
        ]);
      }
      if( is_wp_error( $insertion ) ) {
        throw new Exception('Error writing to the database:');
      }
      if (! empty($client)) {
        $insertion = $db-&gt;insert("peu_client", [
          "uid" =&gt; $uid,
          "data" =&gt; json_encode($client)
        ]);
      }
      if( $insertion ) {
        throw new Exception('Error writing to the database:');
      }
    }
    catch(\Exception $e){
      echo 'Error writing to the database: ',  $e-&gt;getMessage(), "\n";
    }
  }
  
  
  function response_update() {
    $uid = $_POST['GUID'];
    $url = $_POST['url'];
    $programs = $_POST['programs'];
    if (empty($uid) || empty($url) || empty($programs)) {
      wp_send_json(["status" =&gt; "fail","message" =&gt; "missing values"]);
      return wp_die();
    }
  
    try {
      $db = _get_db();
      $insertion = $db-&gt;insert("response_update", [
        "uid" =&gt; $uid,
        "url" =&gt; $url,
        "program_codes" =&gt; $programs
      ]);
      wp_send_json(["status" =&gt; "ok"]);
      wp_die();
      if( $insertion ) {
        throw new Exception('Error writing to the database.');
      }
    }
    catch(\Exception $e)
    {
      echo 'Error writing to the database: ', $e-&gt;getMessage(), "\n";
    }
  }
  
  function _get_db() {
    $host = get_option('statc_host');
    $database = get_option('statc_database');
    $user = get_option('statc_user');
    $password = get_option('statc_password');
    $bootstrapped = get_option('statc_bootstrapped');
  
    $host = (!empty($host)) ? $host : $_ENV['STATC_HOST'];
    $database = (!empty($database)) ? $database : $_ENV['STATC_DATABASE'];
    $user = (!empty($user)) ? $user : $_ENV['STATC_USER'];
    $password = (!empty($password)) ? $password : $_ENV['STATC_PASSWORD'];
    $bootstrapped = (!empty($bootstrapped)) ? $bootstrapped : $_ENV['STATC_BOOTSTRAPPED'];
  
    if (empty($host) || empty($database) || empty($user) || empty($password)) {
      error_log('StatCollector is missing database connection information. Cannot log');
      return new MockDatabase();
    }
  
    $db = new \wpdb($user, $password, $database, $host);
    $db-&gt;show_errors();
  
    if ($bootstrapped !== '5') {
      __bootstrap($db);
    }
    return $db;
  }
  </code></pre>