GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to use GError with a new error domain and error codes?

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
tak



Joined: 15 May 2008
Posts: 3

PostPosted: Fri May 16, 2008 10:37 am    Post subject: How to use GError with a new error domain and error codes? Reply with quote

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: (C)
1
2
3
4

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


Code: (C)
1
2
3
4
5
6
7
8
9
10

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


Code: (C)
1
2
3
4
5
6
7
8
9
10

/**
 * 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;
Back to top
tak



Joined: 15 May 2008
Posts: 3

PostPosted: Fri May 16, 2008 1:35 pm    Post subject: Problem solved Reply with quote

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: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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 */
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP