13.09.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Exception Class<br />

195<br />

In Listing 7.1, you can see that we used a number of methods of the Exception class,<br />

which we discuss shortly.The result of running this code is shown in Figure 7.1.<br />

Figure 7.1<br />

This catch block reports the exception error message <strong>and</strong> notes<br />

where it occurred.<br />

In the sample code, you can see that we raise an exception of class Exception.This builtin<br />

class has methods you can use in the catch block to report a useful error message.<br />

The Exception Class<br />

<strong>PHP</strong> has a built-in class called Exception.The constructor takes two parameters, as we<br />

discussed previously: an error message <strong>and</strong> an error code.<br />

In addition to the constructor, this class comes with the following built-in methods:<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

n<br />

getCode()—Returns the code as passed to the constructor<br />

getMessage()—Returns the message as passed to the constructor<br />

getFile()—Returns the full path to the code file where the exception was raised<br />

getLine()—Returns the line number in the code file where the exception was<br />

raised<br />

getTrace()—Returns an array containing a backtrace where the exception was<br />

raised<br />

getTraceAsString()—Returns the same information as getTrace, formatted as a<br />

string<br />

__toString()—Allows you to simply echo an Exception object, giving all the<br />

information from the above methods

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

Saved successfully!

Ooh no, something went wrong!