PHP Error Handling:Defining an Error Handling Function


Defining an Error Handling Function

If we know that a certain statement or block of code can throw an exception, we should wrap that code within the try…catch block to prevent the default error message being displayed and present a user-friendly error page. But before we proceed, let's create a function that will render an error message and exit the application. As we will be calling it from different script fi les, the best place for this function is, of course, the common.inc.php file.

Our function, called showError(), will do the following:

  • Render a heading saying "Error".
  • Render the error message. We will escape the text with the htmlspecialchars() function and process it with the nl2br() function so that we can display multi-line messages. (This function will convert all line break characters to <br> tags.)
  • Call the showFooter() function to close the opening and tags. The function will assume that the application has already called the showHeader() function. (Otherwise, we will end up with broken HTML.)
 
  Chapter Contents

This excerpt from Chapter NO. 3 "Error Handling" 
from Learning PHP Data Objects by Dennis Popel, is printed with permission from Packt Publishing, Copyright 2007.

 


Related Links
>PHP Interview Questions
>AJAX Interview Questions
>Understanding AJAX
>ASP.NET Tutorial
>ASP.NET Interview questions
>Remoting.Net
>ASP.NET Validation Control
>.NET Assembly
>ADO.NET

 

We will also have to modify the block that creates the connection object in common. inc.php to catch the possible exception. With all these changes, the new version of common.inc.php will look like this:

<?php
/**
* This is a common include file
* PDO Library Management example application
* @author Dennis Popel
*/
// DB connection string and username/password
$connStr = 'mysql:host=localhost;dbname=pdo';
$user = 'root';
$pass = 'root';
/**
* This function will render the header on every page,
* including the opening html tag,
* the head section and the opening body tag.
* It should be called before any output of the
* page itself.
* @param string $title the page title
*/
function showHeader($title)
{
?>
<html>
<head><title><?=htmlspecialchars($title)?></title></head>
<body>
<h1><?=htmlspecialchars($title)?></h1>
<a href="books.php">Books</a>
<a href="authors.php">Authors</a>
<hr>
<?php
}
/**
* This function will 'close' the body and html
* tags opened by the showHeader() function
*/
function showFooter()
{
?>
</body>
</html>
<?php
}
/**

* This function will display an error message, call the
* showFooter() function and terminate the application
* @param string $message the error message
*/
function showError($message)
{
echo "<h2>Error</h2>";
echo nl2br(htmlspecialchars($message));
showFooter();
exit();
}
// Create the connection object
try
{
$conn = new PDO($connStr, $user, $pass);$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
showHeader('Error');
showError("Sorry, an error has occurred. Please try your request
later\n" . $e->getMessage());
}

As you can see, the newly created function is pretty straightforward. The more interesting part is the try…catch block that we use to trap the exception. Now with these modifi cations we can test how a real exception will get processed. To do that, make sure your connection string is wrong (so that it specifi es wrong database name for MySQL or contains invalid fi le name for SQLite). Point your browser to books.php and you should see the following window:

Page 1 | page 2 | page 3



Write your comment - Share Knowledge and Experience


 

Latest placement tests
Latest links
 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring