Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTony Petroski
1,789 PointsTry Catch Exception in PHP
Seeking help with Exceptions in PHP. Playing around with the basics & just cannot trigger a catch.
<?php
require_once 'pdo_connect.php';
//basic mysql PDO connect; works fine.
// $dsn = "mysql:host=localhost;dbname=somedb;charset=utf8";
// $usr = "";
// $pwd = "";
// $db = new \Slim\PDO\Database($dsn, $usr, $pwd);
// $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$sql = 'SELECT username, email FROM user ORDER BY username';
$result = $db->query($sql);
} catch(Exception $e) {
echo $e->getMessage();
echo "Caught Error";
}
Now when i intentionally try to trigger an error numerous ways, i keep getting the full error report (orange background). Just cannot catch them & don't know why.
Even tried placing the require_once file inside the try blocks with a wrong filename or directory path. Same issue. Cannot catch the error. outputs in the orange background.
Im using wamp on Windows OS.
Any idea's?
5 Answers
Tony Petroski
1,789 PointsHi All,
Just worked out that my php.ini file had HTML errors set as ON. This was creating the title blocks but i still cannot catch an error....
Istvan Dobrentei
1,084 PointsHi Tony, Could you show me the error message?
Tony Petroski
1,789 PointsSo recapping on my php.ini file
error_reporting = E_ALL
display_errors = On
html_errors = off (so it displays in text format not html 'orange')
Practice test
<?php
try {
require_once 'C:/wamp/www/PDO/file1/pdo_conneect.php';
die();
} catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
die();
}
// add extra 'e' to pdo_connect.php to trigger catch
Warning: require_once(C:/wamp/www/PDO/file1/pdo_conneect.php): failed to open stream: No such file or directory in C:\wamp\www\PDO\file1\test_error2.php on line 5 Call Stack: 0.0025 238520 1. {main}() C:\wamp\www\PDO\file1\test_error2.php:0 Fatal error: require_once(): Failed opening required 'C:/wamp/www/PDO/file1/pdo_conneect.php' (include_path='.;C:\php\pear') in C:\wamp\www\PDO\file1\test_error2.php on line 5 Call Stack: 0.0025 238520 1. {main}() C:\wamp\www\PDO\file1\test_error2.php:0
as you can see, i cannot capture the error message as per echo statement???
Tony Petroski
1,789 PointsSuccess, this induced $username error was captured by the exception!!!
Does this mean we cannot capture fatal errors s in the previous example?
<?php
try {
ini_set('display_erros', 'On');
$dsn = 'mysql:host=localhost;dbname=somedb';
$username = 'incorrect';
$password = '';
$db = new PDO($dsn, $username, $password);
die();
} catch(Exception $e) {
$result = $e->getMessage();
echo "Message was : " . $result;
var_dump($result);
die();
}
thomascawthorn
22,986 PointsHi Tony Petroski,
I'm pretty sure you can't catch a fatal error.
Try/Catch will catch exceptions - a fatal error doesn't throw an exception because the script dies (It's fatal after all!).
However, you can add something to the script shutdown should a fatal error occur. You'll need to declare a register_shutdown_function.
This function is called every time the script shuts down, not necessarily on a fatal error. Therefore, inside your declared function, you'll need to do:
<?php
$error = error_get_last();
if ($error) {
// Send an error email to dev team etc...
}
Also make sure you set ini params as the very first thing you do:
<?php
ini_set('display_erros', 'On');
// All code down here.
It looks like your error is being thrown because there's a spelling mistake in your include path?
<?php
require_once 'C:/wamp/www/PDO/file1/pdo_conneect.php';
// should be
require_once 'C:/wamp/www/PDO/file1/pdo_connect.php';
Tony Petroski
1,789 PointsThanks again Tom.
Yes the spelling error in the include path was intentional to test the exception class. Now i understand that this class only catches exceptions not fatal script errors. I will continue practicing with the exception & the register_shutdown_function.
Tony Petroski
1,789 PointsTony Petroski
1,789 PointsSorry, had to correct the $db instance line & clean up the formatting.
So i keep getting the orange error title block when i intentionally create a filename or script error. Cannot catch the errors.