| Tutorial PHP Exceptions |
| Article Index |
|---|
| Tutorial PHP Exceptions |
| Page 2 |
| Page 3 |
| Page 4 |
| Page 5 |
Exceptions are an entirely new concept to PHP 5 and represent the object-oriented approach to triggering a nonfatal error in PHP scripts. In PHP 4, the only way to trigger an error was to use the trigger_error() function and then process that error within a custom error handler set by set_error_handler(). Exceptions, on the other hand, enable you to cause errors as well as deal with errors in a much more reasonable fashion.
Understanding the Call Stack
To understand how exceptions work, first you must understand the concept of a call stack. A call stack is, in essence, a record of the order by which functions and methods have been called within your script. Consider the following script example:
<?php
function firstFunction() {
secondFunction();
}
function secondFunction() {
thirdFunction();
}
function thirdFunction() {
echo "Hello, world!";
}
firstFunction();
?>
When this script executes, it is clear that firstFunction() will call secondFunction(), which will in turn call thirdFunction(). What is the call stack when we are within the thirdFunction() call? It would look something like the following:
thirdFunction();
secondFunction();
firstFunction();
The concept of the call stack is an important part of exception handling, as you will see shortly.
| Users' Comments (0) |
|
No comment posted








