Re: error handling in constructor



On Fri, Jan 22, 2010 at 03:17:31PM +0100, Martin Kalbfuß wrote:
> I have the following code to set up a constructor with a filepath
> parameter. As you can see, the construction property filepath, calls a
> function names bitmap_list_load which can return an GError. But how can
> I handle this error inside a constructor? What's your solution for this
> problem. 

1) Return NULL from foo_new() like gdk_pixbuf_new_from_file() does.
Possible, if the users of your objects count with it.

2) Define an `invalid state' of the object so that users always get
non-NULL but they have to query the object state before doing something
meaningful with it and most object methods will have to check the state.
Cumbersome, though in some cases also useful.

If you want to do something that can fail in foo_init() but return NULL
from foo_new() in that case, you can have an `invalid' flag that is used
internally and destroy the constructed object in foo_new() instead of
returning it.  Users constructing the object directly with
g_object_new() can still get objects in the invalid state though.

Yeti



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]