Home arrow PHP arrow Page 3 - Exception Handling in PHP

Methods - PHP

In this second part of a two-part series on error and exception handling in PHP, we focus specifically on handling exceptions. This article is excerpted from chapter 8 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).

TABLE OF CONTENTS:
  1. Exception Handling in PHP
  2. PHP’s Exception-Handling Implementation
  3. Methods
  4. Extending the Exception Class
  5. Catching Multiple Exceptions
By: Apress Publishing
Rating: starstarstarstarstar / 2
January 04, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

Six methods are available to the exception class:

getMessage(): Returns the message if it is passed to the constructor.

getCode(): Returns the error code if it is passed to the constructor.

getLine(): Returns the line number for which the exception is thrown.

getFile(): Returns the name of the file throwing the exception.

getTrace(): Returns an array consisting of information pertinent to the context in which the error occurred. Specifically, this array includes the file name, line, function, and function parameters.

getTraceAsString(): Returns all of the same information as is made available bygetTrace(), except that this information is returned as a string rather than as an array.


Caution Although you can extend the exception base class, you cannot override any of the preceding methods because they are all declared asfinal. See Chapter 6 more for information about thefinalscope.


Listing 8-1 offers a simple example that embodies the use of the overloaded base class constructor, as well as several of the methods.

Listing 8-1. Raising an Exception

try {

    $fh = fopen("contacts.txt", "r");
    if (! $fh) {
        throw new Exception("Could not open the file!");
    }
}
catch (Exception $e) {
   
echo "Error (File: ".$e->getFile().", line ".
          $e->getLine()."): ".$e->getMessage();
}

If the exception is raised, something like the following would be output:

--------------------------------------------

Error (File: /usr/local/apache2/htdocs/8/read.php, line 6): Could not open the file!

--------------------------------------------



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: