12 February 2009

PHP Errors and Exceptions

I learned something about php error handling the other day. I was digging through the docs and discovered how to catch errors, warnings, and such and then re-throw them as exceptions. This is great because I already implemented a default exception handling and logging system to catch all exceptions. Now I can use the same routines for PHP errors. The keys to making this work are the set_error_handler function and the ErrorException class. The set_error_handler has been around for a long time, but I have never really used it. This is a failing on my part. The ErrorException class seems to have been made especially for this type of operation. The ErrorException documentation shows exactly how to use these two things together for object oriented goodness in PHP.
  function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
The function name passed to set_error_handler will be called if there is ever a PHP error. The function set up to handle errors takes the error information, constructs an ErrorException and then throws it. Errors never go directly out to the user or logs, they are always wrapped in an exception. Doing this give you greater control over error handling.

In the user comments on the ErrorException page troelskn gives a suggestion for improvement to the basic example above. He suggests the following code.
function exception_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
This code allows the error reporting value set in php.ini to effect the behavior of the exception_error_handler. Only errors of a high enough severity will be passed on as exceptions. Using his code, if you have warnings suppressed, they will not raise an exception. They will just not be reported. I hope this removes one more of the little PHP quarks for you.

08 February 2009

Extolling the virtues of wool

I can't say enough good things about wool. I have always been told that wool is great for winter camping, but have never really listened. I am now a convert. It all started a few weeks ago when I went to buy some running socks. My old cotton socks were rubbing my feet raw, so I stopped in at Runner's Corner knowing that they would have a good selection of socks. I picked up a couple of pairs of nice socks for running. Then it happened. I saw the wool socks. I remembered that I was going to go on a winter campout in a few weeks, so I looked at the options and picked up a pair of Feetures Bamboo & Wool hiking socks.

I love these socks. They are so comfortable. First off, they come is a size that fits me. The medium size fits shoes sizes 6-8.5. This is great for me because I am a size 8. They fit nice and tight, the way I like it. I don't know why most sock manufactures think that one sock can fit sizes 6-12. It's just silly. Second, the fabric feels great. It took just a little bit of getting used to. It has a different feel then anything I have ever worn. You can feel the fibers in the sock, but they are very soft and nice. Third, no sweat. I don't sweat in these socks. I walk to work most days and when there is a lot of snow on the ground I wear my hiking boots. This is great for the walk, but very hot for inside the building. I have a pair of sandals at work now, but for a while I just wore my boots all day. With my normal cotton socks, my feet were very hot and sweaty, but with these wool socks, no sweat! They help regulate the heat and keep my feet at a comfortable temperature in the heat and the cold. Fourth, they stay dry. I wore my low top hiking shoes with my new socks to work one day last week. It snowed about 4-5 inches that day during work hours. I hiked home through all that snow and then started shoveling the snow when I got home. The bottom of my jeans were soaked. I had snow packed around my ankle. I didn't even notice until I started to take my shoes off. I was completely dry. I shook the snow off my socks and they were still dry too. My conversion was complete.

I went camping this weekend in about a foot of snow. Because of my recent conversion, I went to the local thrift store and picked up a $6 pair of wool suit pants as opposed to the $140 pair of ski pants the salesman at the outfitters store tried to sell me. After tromping through the snow for two days, I was completely dry. Never wet once.

I love wool.