The new try/catch macros - Mailing list pgsql-hackers

From Thomas Hallgren
Subject The new try/catch macros
Date
Msg-id cf5o6h$l00$1@sea.gmane.org
Whole thread Raw
Responses Re: The new try/catch macros
List pgsql-hackers
As I was integrating the new PG_TRY/PG_CATCH/PG_END_TRY macros I 
discovered a couple of minor issues.

1. You use a do {...} while(0) construct to wrap the whole thing. This 
actually makes it impossible to write code that does a try/catch within 
a loop that contains code surrounding it since a continue or break will 
then end up in the wrong place.

2. There's no PG_TRY_RETURN(x) or PG_TRY_RETURN_VOID() macros. Such 
macros are useful when you wish to do a return from within a try/catch. 
Correcting #1 to not use do {...} while(0) it would also be beneficial 
to add PG_TRY_CONTINUE and PG_TRY_BREAK to be able to break out of a 
loop surrounding the try/catch block.

3. IMHO, it's a bit ugly to require parenthesis and semicolons at the 
end of the macros. They are not normally functions (in most languages, 
there are no parenthesis or semicolon except for the catch clause where 
you can define the actual exception variable)

IMHO, you could just remove the do at the beginning and while(0) at the 
end, and add a semicolon to the PT_TRY and PG_CATCH macros to get rid of 
the incorrect continue/break behaviour and normalize the macros somewhat 
so you'd get:

#define PG_TRY_CATCH_POP \    PG_exception_stack = save_exception_stack; \    error_context_stack =
save_context_stack;

#define PG_TRY  \    { \        sigjmp_buf *save_exception_stack = PG_exception_stack; \        ErrorContextCallback
*save_context_stack= error_context_stack;\        sigjmp_buf local_sigjmp_buf; \        if (sigsetjmp(local_sigjmp_buf,
1)== 0) \        { \            PG_exception_stack = &local_sigjmp_buf;
 

#define PG_CATCH  \            PG_TRY_CATCH_POP \        } else { \            PG_TRY_CATCH_POP

#define PG_END_TRY  \        } \    }

#define PG_TRY_CONTINUE { PG_TRY_CATCH_POP continue; }
#define PG_TRY_BREAK { PG_TRY_CATCH_POP break; }
#define PG_TRY_RETURN_VOID { PG_TRY_CATCH_POP return; }
#define PG_TRY_RETURN(x) { PG_TRY_CATCH_POP return (x); }

Now you write things like

PG_TRY
{    ...
}
PG_CATCH
{    ...
}
PG_END_TRY

Loops etc. will be possible to using the PG_TRY_BREAK/PG_TRY_CONTINUE

Thoughts?

Regards,

Thomas Hallgren



pgsql-hackers by date:

Previous
From: "Ross J. Reedstrom"
Date:
Subject: Re: Postgres development model (was Re: CVS comment)
Next
From: "Serguei A. Mokhov"
Date:
Subject: Dead cvsweb link