Throw Exceptions


I pushed some changes out from my development server to my production server and ran into an issue with uploading/moving/resizing photos. It only took about 15 to 20 minutes to track down the issue, but it would have been a lot quicker if I hadn’t been sloppy with my code. In using the Zend Framework a lot, I have really grown to rely on Exceptions being thrown. It really helps track down issues, as you can look through the stack and determine rather quickly and with a fair amount of accuracy where the true problem lies.

In my case, I had a class in my models directory that was creating a folder to store pictures. I wasn’t checking if the folder was created, and when Zend_File_Transfer_Adapter_Http went to move the file from the /tmp directory to the target directory, it threw an exception. But since I didn’t throw an exception when the folder was not created, I started troubleshooting by looking at the Zend Class and trying to determine why it wouldn’t move the file on the production server, but would on the development server. I have since updated my model class to throw an Exception when it can’t create the folder. I altered the permissions and tried it out on my development box, and viola, the Exception was thrown and displayed on screen (would log on prod site), making it very quick and easy to determine what the problem was.

Lesson learned. When you rely on certain actions to be taken, check if they succeed and throw an Exception if they don’t. Now if I could get rid of all my other sloppy coding habits…

Leave a Reply