Home arrow PHP arrow Error Handling in PHP: Introducing Exceptions in PHP 5

Error Handling in PHP: Introducing Exceptions in PHP 5

Welcome to the last part of the series “Error Handling in PHP.” In two parts, this series introduces the basics of error handling in PHP. It demonstrates some of the most common methods for manipulating errors in PHP 4, and explains the implementation of exceptions in PHP 5, particularly in object-oriented environments.

TABLE OF CONTENTS:
  1. Error Handling in PHP: Introducing Exceptions in PHP 5
  2. The basics of exceptions: using the “throw” statement and “try-catch” blocks
  3. Obtaining detailed error information: using built-in Exception class’ additional methods
  4. Working with error types: developing an improved error handling mechanism
  5. Exception subclassing: extending the functionality of the built-in Exception class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 43
January 18, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Introduction

If you spent some time reading the first tutorial of the series, you’ve probably grasped the basic methodologies for handling failures within PHP 4 applications. In that first part, I walked through the use of abrupt “die()” statements, then covered more efficient approaches, such as using “trigger_error()/set_error_handler()” combinations, and finally explained the usage of the popular PEAR_Error object. I tried to illustrate –- at least at a glance -- how different methods can be used to handle potentially conflictive conditions, during the execution of a PHP program.

If you’ve been developing large PHP 4 applications, where very often a centralized error handling mechanism is required, then you may have realized that error manipulating classes can do decent work. However, in PHP 5, things are even better when it comes to handling errors. As you probably know, the latest version of PHP introduces the power and functionality of exceptions, a built-in mechanism for handling program failures through centralized points. This mechanism offers a nice level of flexibility, particularly for delegating error handling to client code and getting detailed information on the errors that occurred and the context within which they happened.

Specifically, this last tutorial will focus on explaining the implementation of exceptions in PHP 5, ranging in from the use of “try-catch” blocks, in addition to subtly more complex structures, such as custom exceptions and exception subclassing. Whether you’re a PHP developer taking the first steps in PHP 5, or a seasoned programmer wanting to fill some gaps in the area of error handling, hopefully this article will be an instructive experience.

Now that you know the topics that will be treated in this article, let’s dive into explaining how PHP 5 exceptions can work for you.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: