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 Integrating PHP with Databases Databases and PHP Handling Exceptions

Siraj Khan
Siraj Khan
3,451 Points

The try catch block is not working.

try {
  $db = new PDO("sqlite:".__DIR__."/databaaase.db");
  var_dump($db);
}
  catch (Exception $e) {
    echo "Unable to connect";
    exit;

}

  echo "Connected to the database";

I have added extra "a" in the PDO class, but the browser is still showing me " object(PDO)#1 (0) { } Connected to the database " . Moreover a new file in the "include" folder of "databaaase.db" is created. The browser should say "Unable to connect" since the code is throwing an exception. Can anyone please tell me, what am I doing wrong here.?

P.S: What is $e, equal to OR infact What does (Exception $e) mean anyway.? We haven't defined this $e variable yet.

4 Answers

jamesjones21
jamesjones21
9,260 Points

Hi,

in your catch block, to output the error, you will need to append the getMessage() method so if any error is encountered when attempting to connect to the database.

The Exception $e means that the $e variable has access to the Exception class, which means that you can then use the getMessage() method.

Also move the echo "Connected to the database" into the try block as this will display when there is a connection else go to the catch block.

try {
  $db = new PDO("sqlite:".__DIR__."/databaaase.db");
  var_dump($db);
echo "Connected to the database";
}
  catch (Exception $e) {
    echo "Unable to connect" . $e->getMessage();
    exit;
}

hope this helps?

Siraj Khan
Siraj Khan
3,451 Points

Hello james,

It's still not catching the error.

Its really confusing and I can't get my head over whatever is happening and why isn't it working. Please help.

jamesjones21
jamesjones21
9,260 Points

can you show me all your code? It will be easier to debug then.

Aquiles Gonzalez
Aquiles Gonzalez
29,457 Points

I had a similar problem and this code helped me ... thank you.

Siraj Khan
Siraj Khan
3,451 Points

By all your code, you mean the entire file.? How do I share the entire file over here.?

Alexander Thieme
Alexander Thieme
1,882 Points

Hi! I have exactly the same problem, and many others seem to have it, too. On my local environment, using Mamp, the catch block is not executed when a spelling error is introduced in the database name (with existing database.db). Instead, the script creates a new .db file and connects to it.

<?php

try {
    $db = new PDO ("sqlite:".__DIR__."/databasewitherror.db");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 
    catch (Exception $e) {
    echo "Unable to connect";
    echo $e->getMessage(); 
    exit;
}

echo "Connected to Database";
Elinor Armsby
Elinor Armsby
13,987 Points

Same thing happens for me on localhost xampp. Anyone find a solution?