13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

196 Chapter 7 Error <strong>and</strong> Exception H<strong>and</strong>ling<br />

You can see that we used the first four of these methods in Listing 7.1.You could obtain<br />

the same information (plus the backtrace) by executing<br />

echo $e;<br />

A backtrace shows which functions were executing at the time the exception was raised.<br />

User-Defined Exceptions<br />

Instead of instantiating <strong>and</strong> passing an instance of the base Exception class, you can pass<br />

any other object you like. In most cases, you will extend the Exception class to create<br />

your own exception classes.<br />

You can pass any other object with your throw clause.You may occasionally want to<br />

do this if you are having problems with one particular object <strong>and</strong> want to pass it through<br />

for debugging purposes.<br />

Most of the time, however, you will extend the base Exception class.The <strong>PHP</strong> manual<br />

provides code that shows the skeleton of the Exception class.This code, taken from<br />

http://us.php.net/manual/en/language.oop5.php, is reproduced in Listing 7.2. Note that<br />

this is not the actual code but represents what you can expect to inherit.<br />

Listing 7.2<br />

Exception class—This Is What You Can Expect to Inherit<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!