A suggestion on PG_TRY() exception handling code.

From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "PGSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: A suggestion on PG_TRY() exception handling code.
Date: 2006-11-16 21:50:49
Message-ID: 65937bea0611161350n5b55dcc6w18d0210e490817e8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

The current usage PG_TRY() looks something like this:

... normal code ...

PG_TRY();
{
... code that might throw ereport(ERROR) ...
}
PG_CATCH();
{
... error recovery code ...
... plus anything that you wish to do even if an error wasn't thrown
...
(because of a PG_RE_THROW possibility)
}
PG_END_TRY();

... do the same thing over again; since either no ERROR or no RE_THROW()
...

... normal code ...

I propose a new constuct, PG_FINALLY. This will help in eliminating code
duplication (hence lesser possibility of errors), and better modularity. It
will also help if someone wishes to call a non-idempotent function in the
now-repeating code.

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

#define PG_CATCH() \
} \
else \
{ \

#define PG_FINALLY() \
} \
{ \
PG_exception_stack = save_exception_stack; \
error_context_stack = save_context_stack

#define PG_END_TRY() \
} \
if (do_re_throw) \
siglongjmp(*PG_exception_stack, 1) \
} while (0)

#define PG_RE_THROW() do_re_throw = true

This would change the semantics to:

... normal code ...

PG_TRY();
{
... code that might throw ereport(ERROR) ...
}
PG_CATCH();
{
... (optional) error recovery code only *and/or* RE_THROW...
}
PG_FINALLY();
{
... do something that you wanted done in any case; ERROR or not ...
}
PG_END_TRY();

... normal code ...

Hope to find buyers.

Best regards,

--
gurjeet[(dot)singh](at)EnterpriseDB(dot)com
singh(dot)gurjeet(at){ gmail | hotmail | yahoo }.com

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Markus Schiltknecht 2006-11-16 22:02:46 Re: replication docs: split single vs. multi-master
Previous Message Bruce Momjian 2006-11-16 21:50:31 Re: replication docs: split single vs. multi-master