GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Nov 27, 2014 7:30 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: How to use GError with a new error domain and error codes?
PostPosted: Fri May 16, 2008 10:37 am 
Offline

Joined: Thu May 15, 2008 8:11 pm
Posts: 3
Hi,
I have difficulties understanding the gnome library instructions on how to use GError in a GTK+ programm written in C. I want to tell the user when the connecting function fails due to the instrument not being detected. I believe this is the sort of error that GError is meant for? What I want to use GError for, is to associate an error message with an error code (DOMAIN_MODULE_ERROR_CODE) that I can then propagate upwards through my program and handle on the top level (with a dialog window if suitable).

I think I understand the propagation of errors via the GError **pointer, but I have difficulties understanding how to set up an error domain and error codes for my own functions. According to library.gnome, I need the following three pieces of code (this is my variation of them), however I am not sure how to link them to gether, i.e. where to put them. At the moment I have them all in a header file and I get a multiple definition error on "pico_comms_error_quark ()".

I basically don't understand how the pieces work together even though I know what they do individually.

Any help would be greatly appreciated, there seems to be very little on this out there on the net.

Code:
/* Error domain for picoammeter specific errors */
#define PICO_COMMS_ERROR pico_comms_error_quark ()


Code:
/**
* Error quark for picoammeter specific errors
*/
GQuark
pico_comms_error_quark (void)
{
  return g_quark_from_static_string ("pico-comms-error-quark");
}


Code:
/**
* Error enum for error codes.
*/
typedef enum
{
   PICO_COMMS_ERROR_NODEVICE, /*< No device found */ 
   PICO_COMMS_ERROR_FAILED   /*< No error code for this error */
} PicoCommsError;


Top
 Profile  
 
 Post subject: Problem solved
PostPosted: Fri May 16, 2008 1:35 pm 
Offline

Joined: Thu May 15, 2008 8:11 pm
Posts: 3
Hi there,

I solved the problem. It wasn't the way I defined my GErrors, it was just the problem of the double definition i.e. I messed up my header files.

The code below seems to work for me. I put it in a separate header file and include it in the file.c where I am working with these error domains.

cheers.
Code:
#ifndef PICO_COMMS_ERROR
/* Error domain for picoammeter specific errors */
#define PICO_COMMS_ERROR (pico_comms_error_quark ())

/**
* Error domain for picoammeter specific errors
*/
GQuark
pico_comms_error_quark (void)
{
   static GQuark q = 0;
   if (q == 0) {
      q = g_quark_from_static_string ("pico-comms-error-quark");
   }
   return q;
}

typedef enum
{
   PICO_COMMS_ERROR_NODEVICE, /*< No device found */ 
   PICO_COMMS_ERROR_FAILED
} PicoCommsError;
#endif /* PICO_COMMS_ERROR */


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group