Exiting from within a PHP exception
Posted March 4th, 2010 in PHP
This is the final post in my series about PHP exceptions and looks at how when an exception occurs, script execution may continue depending on how the exception is handled.
Examples
I've been using PHP's PDO library recently instead of the more traditional approach of using the database specific functions such as the mysql_* functions. PDO is an object oriented approach and uses the exception model to throw errors. It is a useful example to show how execution will continue even if an exception is thrown when attempting to connect to the database.
The two examples below attempt to establish a connection to a MySQL database using a database, login name and password that do not exist, defined as follows:
$dsn = "mysql:host=localhost;dbname=fakedb"; $username = "fakeuser"; $password = "fakepassword";
The first example doesn't bother attempting to catch the exception and execution will halt before getting to the line 'echo "code continues here\n";':
$pdo = new PDO($dsn, $username, $password); echo "code continues here\n";
This will output the following:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'fakeuser'@'localhost' (using password: YES)' in /common/websites/test/exceptions-example.php:7
Stack trace:
#0 /common/websites/test/exceptions-example.php(7): PDO->__construct('mysql:host=loca...', 'fakeuser', 'fakepassword')
#1 {main}
thrown in /common/websites/test/exceptions-example.php on line 7
Because the exception is not handled, the error message will be displayed and execution will halt. Ideally we do not want to display error messages such as the above.
On the other hand, if we handle the exception without ending the script using exit or die:
try {
$pdo = new PDO($dsn, $username, $password);
}
catch(Exception $e) {
echo "Exception\n";
}
echo "code continues here\n";
then this is what will be echoed:
Exception code continues here
In the event of an exception occurring that means the script cannot continue to run, such as not being able to connect to the database, execution should halt (after doing whatever logging or emailed notifications etc is required).
This is often as simple as using exit to end the script's execution like so:
try {
$pdo = new PDO($dsn, $username, $password);
}
catch(Exception $e) {
// do some logging and/or notification here
exit;
}
The PHP Exception Series
This concludes my series on PHP exceptions. See the related posts below for earlier posts in the series.
Related posts:
- Catch mutiple exception types with PHP (Thursday, February 25th 2010)
- Extend PHP's exception object (Thursday, February 18th 2010)
- Replace error reporting with exception handlers with PHP (Thursday, February 11th 2010)
- Catch uncaught exceptions with PHP (Thursday, February 4th 2010)
- PHP Exceptions - available information (Thursday, January 28th 2010)
- PHP Exceptions (Thursday, January 21st 2010)

Comments
blog comments powered by Disqus