title: Try / catch in mysqli
body: <p>I'm following an OOP mysqli course. When connecting to the database, they use the following script:</p>
  
  <pre><code>$db = new mysqli("host", "user", "password", "database");
  if ($db-&gt;connect_error){
      $error = $db-&gt;connect_error;
      echo("Not connected: " . $error);
  }
  </code></pre>
  
  <p>Later though, they call the database connection file with a try / catch block:</p>
  
  <pre><code>try {
      require_once "connection.php";
  }catch (Exception $e){
      $error = $e-&gt;getMessage();
      echo $error;
  }
  </code></pre>
  
  <p>Isn't a possible connection error being handled by the connection file immediately after trying to connect? Is the try / catch block basically doing the same thing? Or is the try / catch block looking for a different type of error?</p>
  
  <p>UPDATE: Just to clarify, after reading some of the answers. When I just do this:</p>
  
  <pre><code>try {
      $db = new mysqli("host", "user", "password", "database");
  }catch (Exception $e){
      $error = $e-&gt;getMessage();
      echo $error;
  }
  </code></pre>
  
  <p>assuming that the database access data is wrong (for example a wrong host), I get a PHP warning but not the error output in the catch block. Shouldn't the catch detect this error? </p>