Thread: catching errors in function

catching errors in function

From
"padmanabha konkodi"
Date:
<p>   <br /> hello sql developers,<br /><br /> i have written function in which while executing it may throw error.<br
/>if the error thrown i want rollback the transaction if not i want commit.<br /><br /> how can i achive this task.<br
/><br/> how can catch exception thrown in the function<br /><br /> plz help me<br /><br /><br /> regards<br />
konkodi<br/><br /><br /><a
href="http://adworks.rediff.com/cgi-bin/AdWorks/sigclick.cgi/www.rediff.com/signature-home.htm/1507191490@Middle5?PARTNER=3"><img
border="0"hspace="0"
src="http://adworks.rediff.com/cgi-bin/AdWorks/sigimpress.cgi/www.rediff.com/signature-home.htm/1963059423@Middle5?OAS_query=null&PARTNER=3"
vspace="0"/></a> 

Re: catching errors in function

From
"Eugene E."
Date:
> i have written function in which while executing it may throw error.
> if the error thrown i want rollback the transaction if not i want commit.


BEGIN;
SELECT you_function(args);
...
many sql commands;
...
COMMIT;

that's all

if your function raises an error
whole transaction will be aborted when the COMMIT is called.


Re: catching errors in function

From
John DeSoi
Date:
On Oct 6, 2005, at 2:55 AM, padmanabha konkodi wrote:

> i have written function in which while executing it may throw error.
> if the error thrown i want rollback the transaction if not i want  
> commit.
>
> how can i achive this task.
>
> how can catch exception thrown in the function


The function itself cannot start or rollback a transaction (functions  
are always executed in the context of a transaction). But I think you  
can use a SAVEPOINT to accomplish your request:

http://www.postgresql.org/docs/8.0/interactive/sql-savepoint.html

And see this page on how to catch exceptions:

http://www.postgresql.org/docs/8.0/interactive/plpgsql-control- 
structures.html#PLPGSQL-ERROR-TRAPPING


John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL