HOW TO NOT HANDLE EXCEPTIONS: GDI+ GENERIC EXCEPTION Posted on 06/09/10 at 1:13PM
[ mandlar ]

A few months ago at work, I came across the worst possible exception I have ever seen thrown by a program:

A generic error occurred in GDI+

For those who don’t know, GDI+ is the graphics library for Windows that is supported in .NET via the System.Drawing namespace.

So, what is so bad about this?  It doesn’t tell me why this error occurred!  There is no inner exception passed along.  If I don’t know the source of the problem that is causing the exception, then how can I prevent the exception from being thrown?  Heck, how can I even catch this exception and know what to do with it? Did the memory get full?  Was a file not found? Is a file being used by another process? Did the world blow up?  WHAT THE HELL HAPPENED?

After a quick search on StackOverflow I came up with a number of possible sources of the problem, but at best we are guessing because the exception tell us absolutely nothing!

And the list goes on and on with further suggestions.  The thing that baffled me is that my GDI+ exception occurs randomly.  It doesn’t happen every single time I run my GDI+ functions.  I think it has to do with some sort of file lock going on, but honestly, I have no idea because the GDI+ generic exception is a worthless piece of information.  I re-wrote my code to ensure that all file handles are closed after being used.  I even tried copying the bitmap created from a file into a NEW bitmap to remove the dependency between the bitmap and the original file.  Nothing.

Personally, I would love to meet the developer who thought swallowing up the inner exceptions was a fabulous idea and reprimand the hell out of him or her. And why hasn’t Microsoft updated the library with better exceptions?

So, the moral of the story folks is to don’t write code like this:

1
2
3
4
5
6
7
8
try
{
     // one or a million lines of code
}
catch (Exception ex)
{
     throw new Exception("A generic error occured in [program name]!");
}

Wednesday, June 9th, 2010 at 1:13 pmand is filed under computers. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “HOW TO NOT HANDLE EXCEPTIONS: GDI+ GENERIC EXCEPTION”

  1. Fotograf Oslo Says:
    May 14th, 2011 at 3:11 am

    HURRAY! can’t troubadour. due to himself thus ever so.

  2. Manuela Schub Says:
    September 6th, 2011 at 5:56 pm

    Thanks for this blog. Thats all I will say. You most undoubtedly have made this blog into something thats eye opening and important. You clearly apprehend so abundant regarding the subject, youve lined so several bases. Nice stuff from this half of the internet.

Leave a Reply