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.

Exceptions in Bob’s Auto Parts<br />

199<br />

This example is fairly simple. In the next section, we look at ways to create different<br />

exceptions to deal with different categories of error.<br />

Exceptions in Bob’s Auto Parts<br />

Chapter 2,“Storing <strong>and</strong> Retrieving Data,” described how you could store Bob’s order<br />

data in a flat file.You know that file I/O (in fact, any kind of I/O) is one area in programs<br />

where errors often occur.This makes it a good place to apply exception h<strong>and</strong>ling.<br />

Looking back at the original code, you can see that three things are likely to go<br />

wrong with writing to the file: the file cannot be opened, a lock cannot be obtained, or<br />

the file cannot be written to.We created an exception class for each of these possibilities.<br />

The code for these exceptions is shown in Listing 7.4.<br />

Listing 7.4<br />

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

Saved successfully!

Ooh no, something went wrong!