Programming Basics

  Home arrow Programming Basics arrow Exception Handling in PHP
PROGRAMMING BASICS

Exception Handling in PHP
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating:  stars stars stars stars stars / 0
    2011-09-07

    Table of Contents:
  • Exception Handling in PHP
  • Methods of the Exception Class

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Exception Handling in PHP


    (Page 1 of 2 )

    This fourth part of a five-part article series on error and exception handling focuses on exception handling in PHP, and methods of the exception class. It's excerpted from chapter eight of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

    PHP’s Exception-Handling Implementation

    This section introduces PHP’s exception-handling feature. Specifically, we’ll touch upon the base exception class internals, and demonstrate how to extend this base class, define multiple catch blocks, and introduce other advanced handling tasks. Let’s begin with the basics: the base exception class.

    PHP’s Base Exception Class

    PHP’s base exception class is actually quite simple in nature, offering a default constructor consisting of no parameters, an overloaded constructor consisting of two optional parameters, and six methods. Each of these parameters and methods is introduced in this section.

    The Default Constructor

    The default exception constructor is called with no parameters. For example, you can invoke the exception class like so:

    throw new Exception();

    Once the exception has been instantiated, you can use any of the six methods introduced later in this section. However, only four will be of any use; the other two are useful only if you instantiate the class with the overloaded constructor, introduced next.

    The Overloaded Constructor

    The overloaded constructor offers additional functionality not available to the default constructor through the acceptance of two optional parameters:

    1. message: Intended to be a user-friendly explanation that presumably will be passed to the user via thegetMessage()method, introduced in the following section.
    2. error code: Intended to hold an error identifier that presumably will be mapped to some identifier-to-message table. Error codes are often used for reasons of internationalization and localization. This error code is made available via thegetCode()method, introduced in the next section. Later, you’ll learn how the base exception class can be extended to compute identifier-to-message table lookups.

    You can call this constructor in a variety of ways, each of which is demonstrated here:

    throw new Exception("Something bad just happened", 4)
    throw new Exception("Something bad just happened");
    throw new Exception("",4);

    Of course, nothing actually happens to the exception until it’s caught, as demonstrated later in this section.

    More Programming Basics Articles
    More By Apress Publishing

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - The Transliteration Operator in Perl
    - Perl String Processing Functions
    - Perl String Processing
    - Control Flow Constructs: Loops Conclusion
    - Loop Control Constructs
    - Control Flow Constructs: the For and Foreach...
    - Loops and Control Flow Constructs
    - Expression Modifiers for Perl Control Flow C...
    - Logical Operators and Control Flow Constructs
    - Comparing Strings with Control Flow Construc...
    - Perl Operators and Control Flow Constructs
    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP


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