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 trial

PHP

Tony Petroski
Tony Petroski
1,789 Points

Try 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?

Tony Petroski
Tony Petroski
1,789 Points

Sorry, had to correct the $db instance line & clean up the formatting.

<?php

require_once 'pdo_connect.php'; 
// basic mysql PDO connect; works fine. 
// $dsn = "mysql:host=localhost;dbname=somedb;charset=utf8"; // $usr = ""; // $pwd = ""; 
//$db = new PDO($dsn, '', '');
// $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"; 
}

So i keep getting the orange error title block when i intentionally create a filename or script error. Cannot catch the errors.

5 Answers

Tony Petroski
Tony Petroski
1,789 Points

Hi 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
Istvan Dobrentei
1,084 Points

Hi Tony, Could you show me the error message?

Tony Petroski
Tony Petroski
1,789 Points

So 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
Tony Petroski
1,789 Points

Success, 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();

}

Hi 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
Tony Petroski
1,789 Points

Thanks 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.