Edit: I already tryed what is explained in the "possible duplicate" post. Didn't work. And as i explained, the exception is catched almost always except for some rare case. The example in my answer is one of that cases and it think that is related to malformed SQL statement. But i need to catch the exception, not to solve it.
I've looked everywhere and cannot find a resolution to my problem. Using PDO i'm able to catch almost every exception but sometimes it doesn't work.
here the code i use
Helper class to get PDO instance:
<?PHP
class DBH {
protected static $cn;
private function __construct() {
//HERE I REMOVED DATABASE VARIABLES FOR PRIVACY
$dsn = "mysql:host=$dbIp;port=$dbPort;dbname=$dbDbase";
try {
self::$cn=new PDO($dsn, $dbUte, $dbPass);
self::$cn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) { $this->exErr($e->getMessage(),"DbHelper __construct()"); }
}
public static function getDatabaseHandler() {
if (!self::$cn) {
//new connection object.
new DBH();
}
//return connection.
return self::$cn;
}
private function exErr($msg,$func) {
$response["result"] = "error";
$response["function"] = $func;
$response["errmsg"] = $msg;
echo json_encode($response);
exit;
}
}
?>
Here is an example function:
<?PHP
include_once 'DBH.php';
class Example {
private $cn;
private function __construct() {
$this->cn = DBH::getDatabaseHandler();
}
private function exampleFunction() {
try {
$query = $this->cn->prepare("DELETE FROM examples WHERE did= ? AND (kind='examplekind' AND action != 0 AND action != 2 AND action != 13) OR (kind='examplekind2' AND action != 0 AND action != 2 AND action != 13) OR kind='exampleKind3'");
$query->execute(array($this->uid));
} catch (PDOException $e) { $this->exErr($e->getMessage(),"exampleFunction()"); }
}
private function exErr($msg,$func) {
$response["result"] = "error";
$response["function"] = $func;
$response["errmsg"] = $msg;
echo json_encode($response);
exit;
}
}
?>
As i said everything works as expected except for few cases like the one above. If i use the query in the exampleFunction()
a Fatal Error is thrown but not catched by the exception. Someone can explain me why? Thanks