Error Handling in PHP

«»

SHARE & COMMENT :

Types of Error Handling in PDO

By default, PDO uses the silent error handling mode. This means that any error
that arises when calling methods of the PDO or PDOStatement classes go unreported.
With this mode, one would have to call PDO::errorInfo(), PDO::errorCode(),
PDOStatement::errorInfo(), or PDOStatement::errorCode(), every time an
error occurred to see if it really did occur. Note that this mode is similar to traditional
database access—usually, the code calls mysql_errno() and mysql_error() (or
equivalent functions for other database systems) after calling functions that could
cause an error, after connecting to a database and after issuing a query.

Another mode is the warning mode. Here, PDO will act identical to the traditional
database access. Any error that happens during communication with the database
would raise an E_WARNING error. Depending on the confi guration, an error message
could be displayed or logged into a fi le.

Finally, PDO introduces a modern way of handling database connection errors—by
using exceptions. Every failed call to any of the PDO or PDOStatement methods will
throw an exception.

As we have previously noted, PDO uses the silent mode, by default. To switch to a
desired error handling mode, we have to specify it by calling PDO::setAttribute()
method. Each of the error handling modes is specifi ed by the following constants,
which are defi ned in the PDO class:

  • PDO::ERRMODE_SILENT – the silent strategy.
  • PDO::ERRMODE_WARNING – the warning strategy.
  • PDO::ERRMODE_EXCEPTION – use exceptions.

To set the desired error handling mode, we have to set the PDO::ATTR_ERRMODE
attribute in the following way:


	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

To see how PDO throws an exception, edit the common.inc.php fi le by adding the
above statement after the line #46. If you want to test what will happen when PDO
throws an exception, change the connection string to specify a nonexistent database.
Now point your browser to the books listing page.

You should see an output similar to:

This is PHP’s default reaction to uncaught exceptions—they are regarded as fatal
errors and program execution stops. The error message reveals the class of the
exception, PDOException, the error description, and some debug information,
including name and line number of the statement that threw the exception. Note
that if you want to test SQLite, specifying a non-existent database may not work as
the database will get created if it does not exist already. To see that it does work for
SQLite, change the $connStr variable on line 10 so that there is an illegal character in
the database name:


	$connStr = 'sqlite:/path/to/pdo*.db';

Refresh your browser and you should see something like this:

As you can see, a message similar to the previous example is displayed, specifying
the cause and the location of the error in the source code.

«»

Comments

comments

Pages: 1 2 3 4

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better