Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:34:29+00:00 2026-06-15T03:34:29+00:00

I can’t know how can i catch PDOException in the follow code,please tell me

  • 0

I can’t know how can i catch PDOException in the follow code,please tell me where throw exception in the follow code?

i have (directory) :

 - folder
     -1) b.php
     -2) c.php

 - autoloader

in the b.php :

<?php
namespace folder;
use folder as x;
require_once '../autoload.php';
class b{
    function __construct(){
        print("<p>you are in class b<p/>");
    }
}
$t=new x\c();
?>

and in c.php:

class c{
    function __construct(){
        print("<p>you are in class c<p/>");

        if(DB_TYPE == 'mysql')  
        $pdoString=DB_TYPE.':dbname='.DB_NAME.';host='.DB_HOST;

        $pdoUsername=DB_USERNAME;
        $pdoPass='1';//DB_PASS; in this line I enter wrong password

    try{
            $this->pdo = new PDO($pdoString, $pdoUsername, $pdoPass);

    }catch(PDOException $e){ //we can't catch exception here!
        die('<p> Error DataBase Connection: '.$e->getMessage()."</p>");
    }
}
}
?>

I enter wrong password,i expect that have catch the exception in my try catch block but have this output:

 you are in class c

 Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)' in C:\xampp\htdocs\TEST\folder\c.php:17 Stack trace: #0 C:\xampp\htdocs\TEST\folder\c.php(17): PDO->__construct('mysql:dbname=kn...', 'root', '1') #1 C:\xampp\htdocs\TEST\folder\b.php(10): folder\c->__construct() #2 {main} thrown in C:\xampp\htdocs\TEST\folder\c.php on line 17
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:34:30+00:00Added an answer on June 15, 2026 at 3:34 am

    You are in a namespace. Hence, PHP will look for the namespaced class folder\PDOException and attempt to catch that (And fail).

    To use the global namespace, simply add a backslash behind the class name:

    catch (\PDOException $e) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can you please tell me why there are two log files created on tomcat
Can someone please explain to me why the output from the following code is
Can someone maybe tell me why this is not working? I have used echo
Can anybody tell me a regular expression to use within some PHP to find
Can someone please tell me is there a way to solve difference equation e.g:
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say
Can any one tell, how to get the result of LINQ query contains group
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.