Monday, June 09, 2008

Properly Naming Exceptions Is Important

I just got done spending over an hour debugging an OutOfMemoryException in a C# application I have. It was a really peculiar error because it began spontaneously in an application that primarily just iterates over about fifty thousand images in a repository and resizes them for the web. This was peculiar because the application had already run succesfully many times for the same data set with no memory exception.

Regardless, I dutifully set up lots of different memory freeing mechanism, going to the final state of forcing a Image.dispose() and forced garbage collection after every single image. Still OutOfMemory. DANG YOU!!!

Ok, so what it turns out is that if you are attempting to load a corrupted image file using C#'s Image.FromFile(), it crashes with the very unhelpful OutOfMemoryException instead of something more useful like ImageCorruptedException. Heck, I would have settled for IAmTooLazyToThinkOfAnActualException, at least that wouldn't have misled me so much :)