本文介紹了PDOException 沒有被捕獲?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在 PHP 中遇到以下錯誤:
I'm getting the following error in PHP:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'localhost' (10061)' in C:xampphtdocsprojectServiceDatabase.class.php:26 Stack trace: #0 C:xampphtdocsprojectServiceDatabase.class.php(26): PDO->__construct('mysql:host=loca...', 'root', '', Array) #1 C:xampphtdocsprojectServiceDatabase.class.php(54): ServiceDatabase::initialize() #2 C:xampphtdocsprojectindex.php(15): ServiceDatabase::getHandler() #3 {main} thrown in C:xampphtdocsprojectServiceDatabase.class.php on line 26
錯誤本身不是問題,我故意終止了MySQL服務在 Windows 中查看發(fā)生了什么(我正在使用 XAMPP).問題是我無法捕捉到 PDO 的異常對象拋出,我不知道為什么.
The error itself is not the problem, I intentionally terminated the MySQL service in Windows to see what happened (I'm using XAMPP). The problem is that I'm unable to catch the exception that the PDO object throws and I don't know why.
try {
$host = "localhost";
$dbname = "project";
$userName = "root";
$password = "";
$charset = "utf8";
$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
$driverOptions = array(
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $charset"
);
// This is the line that supposedly throws the exception (LINE 26):
$dbh = new PDO($dsn, $userName, $password, $driverOptions);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::setHandler($dbh);
} catch (PDOException $e) {
die("CATCHED"); // This line is never reached
} catch (Exception $e) {
die("CATCHED"); // nor this one.
}
我在這里遺漏了什么?
推薦答案
我唯一能想到的是如果你在一個命名空間的類中,并且應該使用 PDOException
而不是 >PDOException
.
The only thing I can think of is if you're inside a namespaced class, and should use PDOException
instead of PDOException
.
這篇關于PDOException 沒有被捕獲?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!