Skip to content

Instantly share code, notes, and snippets.

Created May 29, 2015 04:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/a5d892c310634ce5c28c to your computer and use it in GitHub Desktop.
Save anonymous/a5d892c310634ce5c28c to your computer and use it in GitHub Desktop.
PHP try catch error handling
<pre>
<?php
try {
echo "Try to do something here.<br />";
throw new Exception("Yikes. Something went funky.<br />", 234);
} catch (Exception $e) {
//print_r($e);
echo $e->getMessage();
} finally {
echo "This will always run in our try block.<br />";
}
// try {
// // connect (create a new MySQLi object)
// $mysqli = new MySQLi('localhost', 'root', 'root', 'snippet');
// if (mysqli_connect_error()) {
// throw new Exception(mysqli_connect_error());
// }
// $sql = "SELECT * FROM search LIMIT 10";
// $result = $mysqli->query($sql);
// if (!$result) {
// throw new Exception($mysqli->error);
// }
// while($row = $result->fetch_object()) {
// $results[] = $row;
// }
// }
// catch (Exception $e)
// {
// echo 'We are having issues. Here is how to get help.';
// }
// print_r($results);
?>
</pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment