catch block in a try/catch not working

My code sample is simply trying to connect to a database:

try {
    $db = new mysqli('localhost', 'username', 'password', 'database');
    if ($db->connect_error) {
        $error = $db->connect_error;
        echo "From try: " . $error . "<br/>";
    } else {
        echo "From try: Success!" . "<br/>";
} catch(Exception $e) {
    echo "From catch: " . $error . "<br/>";

Open in new window

I'd intentionally put in the wrong user name or password, and it bombs out as expected. What I don't understand is why the catch block is not catching the error because it's the statements in the try block that's displaying the error. Can someone explain to me why this is the case?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA EngineerCommented:
The database code is not throwing an exception. It is returning an error which you are testing in the if statement.
elepilAuthor Commented:
To Mark Bullock. Thanks for responding.

That's what I suspected, but the PHP page actually reports the error with its typical orange table block. It is common for database-related function to throw exceptions, as that is what I've become accustomed to in Java. If it isn't throwing exceptions on a critical function as an incorrect username and password to the database, when would it throw exceptions?
Mark BullockQA EngineerCommented:
You can enable exceptions using:

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilAuthor Commented:
That was what I needed, thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Network Analysis

From novice to tech pro — start learning today.