Note that there are some explanatory texts on larger screens.

plurals
  1. POCatching PDOException in lower layer and re-throwing as different exception to upper layer
    primarykey
    data
    text
    <p>Hello I am writing small PHP framework using PDO library. So at this moment I am facing with the exception catching problem. I want to catch thrown PDOException in lower layer and re-throw it as different exception object and bubble it up in such way to upper layer, hiding sensitive error information from users and displaying abstract useful messages for site admins and etc. In the example code (Mssql.class.php) I am trying to catch exception and re-throw as DBIException object when the connection to database cannot be established (e.g. bad hostname or username). </p> <p>In the upper layer I try to catch DBIException (DBI.class.php) and re-throw it as CoreException object. In the last layer (SystemLoader.class.php) I am trying to catch CoreException and print corresponding message. As you see from the code the main initialization class (SystemLoader.class.php) and init() method are executed from index.php but if PDOException error occurs in lower layers I got Fatal error: Uncaught exception 'PDOException' not CoreException with message "System couldn't load specified resources" as I am expecting. But If I put System::init() in index.php between try..catch block the exception is caught but not the DBIException or CoreException only PDOException. </p> <p>It looks like that catching mechanism somehow is not responding to try...catch blocks in lower level and the other thing is that I don't want to use try...catch blocks in index.php file simply I don't want to make this file structure complex. I read many info in the internet and understood the concept of Exception handling but at this moment I don't know where I am doing mistake. Should I set custom Exception Handler in SystemLoader.class.php or use some other technics</p> <h1>index.php</h1> <pre><code>&lt;?php require_once("SystemLoader.class.php"); System::init(); //initializing system params and other stuff ?&gt; </code></pre> <h1>SystemLoader.class.php</h1> <pre><code>&lt;?php require_once("DBIException.class.php"); class SystemLoader { static function init() { try { DBI::loadDBDriver("mssql"); //trying to load DB driver } catch(CoreException $e) { echo "System couldn't load specified resources"; } } } ?&gt; </code></pre> <h1>DBI.class.php</h1> <pre><code>&lt;?php require_once("CoreException.class.php"); class DBI { private $dbh; public static function loadDBDriver($dbDriver) { if($dbDriver = "mssql") { try { return new Mssql; } catch (DBIException $e) { throw new CoreException($e) } } elseif($dbDriver = "mysql") { try { return new Mysql; } catch (DBIException $e) { throw new CoreException($e) } } } public function setDBHandle($dbh) { $this-&gt;dbh = $dbh; $this-&gt;dbh-&gt;setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); $this-&gt;dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this-&gt;dbh-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); } } </code></pre> <h1>Mssql.class.php</h1> <pre><code>&lt;?php require_once("DBIException.class.php"); class Mssql extends DBI { public function __construct() { try { $dbh=new PDO("dblib:host=192.168.0.1;Database=dbName", "user", "pass"); parent::setDBHandle($dbh); } catch(PDOException $e) //PDOException is not catched at this point { throw new DBIException($e); /re-throw as new Exception object } } } ?&gt; </code></pre> <h1>CoreException.class.php</h1> <pre><code>&lt;?php class CoreException extends Exception {} ?&gt; </code></pre> <h1>DBIException.class.php</h1> <pre><code>&lt;?php class DBIException extends Exception {} ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload