Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > exception handling mechanism

Reply
Thread Tools

exception handling mechanism

 
 
Ralph A. Moeritz
Guest
Posts: n/a
 
      04-12-2005
Hello,

I'd like to implement an exception handling mechanism
in my C programs. Up to now I have been using a local
error handling scheme where errors are handled (when
appropriate) in the lowest-level functions eg.

char *
foo (void)
{
int err = 0; /* (error flag: 1=set) */
char *str = xmalloc (BUF_LEN);

int ret = do_something (str);
if (!ret)
{
++err;
goto clean_up;
}

clean_up:
if (err)
{
/* handle error */
}

return str;
}

void
bar (void)
{
char *s = foo ();
/* ... */
}

What this sheme lacks is a way for the called function to pass error
information back to the calling function so that it can deal with it
in appropriate situations. I've heard of people implementing TRY,
CATCH, THROW using setjmp & longjmp, but I've never seen any code
for this...

Regards,
Ralph

 
Reply With Quote
 
 
 
 
Fred L. Kleinschmidt
Guest
Posts: n/a
 
      04-12-2005


"Ralph A. Moeritz" wrote:
>
> Hello,
>
> I'd like to implement an exception handling mechanism
> in my C programs. Up to now I have been using a local
> error handling scheme where errors are handled (when
> appropriate) in the lowest-level functions eg.
>
> char *
> foo (void)
> {
> int err = 0; /* (error flag: 1=set) */
> char *str = xmalloc (BUF_LEN);
>
> int ret = do_something (str);
> if (!ret)
> {
> ++err;
> goto clean_up;
> }
>
> clean_up:
> if (err)
> {
> /* handle error */
> }
>
> return str;
> }
>
> void
> bar (void)
> {
> char *s = foo ();
> /* ... */
> }
>
> What this sheme lacks is a way for the called function to pass error
> information back to the calling function so that it can deal with it
> in appropriate situations. I've heard of people implementing TRY,
> CATCH, THROW using setjmp & longjmp, but I've never seen any code
> for this...
>
> Regards,
> Ralph


Why make life complicated? There are two extremely simple solutions:
1)
int foo( char **result ) {
if ( result == NULL ) return ERROR_1; /* #define ERROR_1 somewhere */
*result = malloc(...);
...
if (error) return ERROR_2;
...
return 0;
}

void bar(void) {
char *s;
int status = foo(&s);
}

2)
char *foo( int *status )

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception as the primary error handling mechanism? Peng Yu Python 95 01-07-2010 07:30 PM
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Event Handling and Signal-Slot Mechanism Steven Woody Python 2 01-20-2009 02:33 AM
Best error handling mechanism? Rob Hoelz Perl Misc 3 06-16-2007 09:35 AM
confusion about Exception Mechanism Ben Finney Python 3 01-15-2004 06:06 AM



Advertisments