Broad Network


The Predefined Exception Object in PHP

Exception Handling in PHP - Part 2

Forward: In this part of the series, we look at the predefined exception object in more detail.

By: Chrysanthus Date Published: 11 Aug 2012

Introduction

This is part 2 of my series, Exception Handling in PHP. PHP has a predefined exception class called Exception. It is there for you to use. It makes the handling of exception easy. We saw a bit of it in the previous part of the series. In this part of the series, we look at the predefined exception object in more detail.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Properties and Methods
I will give you the properties of the exception class and most of its methods. In simple terms, the description of the exception class is:

class Exception
    {
        protected $message = 'Unknown exception';   // string exception message
        protected $code = 0;                        // integer user defined exception code
        protected $file;                            // string source filename of exception
        protected $line;                            // string source line of exception
    
        function __construct([$message = null[, $code = 0]]);

        final function getMessage();                // message of exception
        final function getCode();                   // code of exception
        final function getFile();                   // source filename
        final function getLine();                   // source line
    }

The built-in class has four properties. It has a constructor function with the error message and code parameters. These two parameters have default values. They are each optional, when programming. When you are instantiating an exception object with the operator, new, as we saw in the previous part of the series, if you do not type any of the arguments, the default value would be used. For the error message the default value is NULL, so “Unknown exception” is used. For the code the default value is zero.

For the four properties, the code is an integer. The other three properties are strings. To read any of the values of the four properties, the four methods beginning with “final” are used: one method for each property.

Now, read through all the members of the class and the comment for each member. The values for the message and code properties are given during instantiation of the Exception object with the new operator. They are given as arguments to the constructor function as we saw in the previous part of the series. The value for the source filename property of the source file (PHP) that has the error and the value for the line number property of the error line in the source file, are determine for you automatically by the Exception class internal PHP coding. You just read them with the corresponding methods above.

Example
We shall now look at an example that illustrates the use of all the properties and methods above. Remember, the code integer value is value that you the programmer give. You choose the value, based on the context of the error; the value you choose is subjective. The code is dealing with division by zero, and the value of zero is used as the error code number.

<?php

    $numerator = 8;
    $denominator = 0;

        try
            {
                if ($denominator != 0 )
                    {
                        $answer = $numerator/$denominator;
                        echo $answer;
                    }
                else
                    {
                        throw new Exception("Division by zero is not allowed!", 0);
                    }
            }
        catch (Exception $e)
            {
                echo "Error Message is: ".$e->getMessage()."<br />";
                echo "Error Code is: ".$e->getCode()."<br />";
                echo "File with Error is: ".$e->getFile()."<br />";
                echo "Error line number in file is: ".$e->getLine()."<br />";
            }

?>

This is the result from my computer.

Error Message is: Division by zero is not allowed!
Error Code is: 0
File with Error is: C:Program FilesApache Software FoundationApache2.2htdocstemp.php
Error line number in file is: 15

The result from my computer says the error is at line 15 where the throw statement is typed.

Well, we can stop here. We continue in the next part of the series.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course

Comments

Become the Writer's Fan
Send the Writer a Message