Login |  Register 
Your PHP skills tool showed me were I needed to improve and how to do it!
Tom
 

Try...catch

The try catch blocks allow you to handle exceptions in a controlled manner. However while these blocks will catch an error it is up to you to deal with them properly. It is much better to stop an error occurring rather than catching it after it has occurred The first reason for this is it makes your code, more reliable by defensively programming. The second reason is that it takes more time for the code to recover from a exception even when it is in a try...catch block. The try catch block will catch and deal with any errors within the try block.
$person = null;
try
{
 echo $person->name;
}
catch(Exception $ex)
{
 echo "An error occured: " . $ex->getMessage();
}
So above null exception is caught on the $person variable. You can throw a exception by creating a new exception object, like so...
$person = null;
.
.
.
if (!$person)
{
 throw new Exception("The person object can't be null.");
}
You can also define your own exception types, for instance a type for your database code. So something like.
class DatabaseException extends Exception
{
    // Make the message parameter required, and hide the code parameter
    public function __construct($message) {
        parent::__construct($message, 0);
    }

    // To change the string output override this
    public function __toString() {
        return "An error occured in your database, the error is: {$this->message}n";
    }
}
By overriding the __toString function it allows the text that is output to the screen to be modified. This can then be called in the following way...
throw new DatabaseException("Invalid SQL query.");
When catching errors more than one catch block can be present for a try block, so that different exceptions can be handled in different ways.
try
{
 /*
  * Code to catch
   */
}
catch (DatabaseException $e)
{
 echo "An error occured: " . $e->getMessage();
}
catch (Exception $e)
{
 /*
  * Log general exception
  */
}
As you can see from above general exceptions are logged and database exceptions are shown on screen. If a exception isn't caught a Fatal error will be thrown by PHP. You can catch uncaught exception in PHP by using the set_exception_handler function.
function HandleException($exception)
{
  include('header.php');
  include('errorPage.php');
  echo "An error occured when trying to carry out your request.nnThe error is: " , $exception->getMessage(), "n";
  include('footer.php');
}

set_exception_handler('HandleException');
Therefore as you can see above the set_exception_handler function is passed the HandleException function as a parameter which it will call if a error occurs. In this case the HandleException function calls the templates needed to show a nice error caught page to the user. It is recommended that you implement a exception handler in your base code of your site to ensure that the user is never shown any ugly error messages.

No comments have been provided.
security image
Written by Dominic Skinner
Last Updated: 2011-10-25 16:00:38