[Mono-dev] Howto improve Error-Messages of GDI-Functions?

Sebastien Pouliot sebastien.pouliot at gmail.com
Wed Feb 28 14:58:30 EST 2007


Hey,

On Wed, 2007-02-28 at 19:42 +0100, latency at gmx.de wrote:
> I recently ran into a problem where Bitmap.GetPixel(int, int) threw 
> ArgumentException with the Message: "Invalid Parameter. A null reference or 
> invalid value was found."
> 
> As you can image, that does not help at all when debugging your application.

It may not be precise but it's hardly not helping "at all" !?!

I'm sure people doing C find this very helpful when compared to
functions that would return a BOOL or void. I know I do ;-) even if an
error code is primitive compared to a nice stack trace with source code
line numbers.

> Therefore I've made a quick fix which helped me to have a better exception 
> message (btw. the path implements behavior which is specified in MS 
> documentation of GetPixel),

Don't trust documentation as it's often incorrect. The only way to be
sure about the errors/exceptions are to write a few unit tests. 

The first ones for the managed Bitmap.GetPixel method (which will
confirm which exception should be thrown) and next for the unmanaged
(wrapped) GDI+ function, GdipBitmapGetPixel.

The goal is to both exception type (System.Drawing.dll) and error code
(libgdiplus) compatible with MS implementation.

>  but still, is there a way to improve the 
> exceptions messages of gdi+ in general? 

All GDI+ functions return a GpStatus (int) with *very* generic (and
often misleading) error codes. This results get mapped into an exception
by System.Drawing - however the message can't describe the specifics of
the error.

It has been suggested to add a few functions in libgdiplus to keep more
information about errors, which our System.Drawing.dll could use to
retrieve more meaningful exception messages.

However this leads to other issues. It wouldn't work under Windows (not
that I really mind this one), it would require us, someday, to add
localization to libgdiplus (so far this isn't required for anything
else), it requires to add code (and strings) to every function in
libgdiplus (yuck) and, finally, it's not a complete solution as it
doesn't cover all cases (e.g. errors coming from Cairo won't have any
more information).

Another approach would be to duplicate the GDI+ checks in
System.Drawing.dll and throw exceptions with more useful messages. We
already do that in some cases, where we don't have the choice (*), but
in general we avoid double checks.

(*) e.g. if the GpStatus doesn't map to the thrown exception - which is
where the unit tests come handy

> Or do we have to do what I've done with GetPixel?

It depends. Like I said some times we just don't have the choice.
Generally this indicates that MS does the (same) check(s) in both
System.Drawing.dll and GDI+ (uncommon) and we can (well must for
compatibility) throw the same exception type (and are free to use a more
meaningful error message).


As for your patch, please provide unit tests showing the documented
behavior is the correct one and I'll commit them (fix+tests) to SVN.

Thanks,
Sebastien

> 
> Kind Regards,
> Valentin S.
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
-- 
Sebastien Pouliot  <sebastien at ximian.com>
Blog: http://pages.infinit.net/ctech/




More information about the Mono-devel-list mailing list