Thread: Re: trap for any exception
> > I read 37.7.5. Trapping Errors, but the syntax requires to write > explicitly the exception condition, and not a generic one. > > Is it possible to build a generic trap or do you know a workaround for > that ? > Sorry, I found the OTHERS condition that trap any error. But the question still remains to find how could I interpret it and found the error code and message ? Sabin
Hi there, I'd like to build a PL/pgSQL function which is able to generic trap any error, and interpret it. I read 37.7.5. Trapping Errors, but the syntax requires to write explicitly the exception condition, and not a generic one. Is it possible to build a generic trap or do you know a workaround for that ? TIA, Sabin
That's ok, I found SQLSTATE and SQLERRM.
On Thu, 15 Nov 2007, Sabin Coanda wrote: > Hi there, > > I'd like to build a PL/pgSQL function which is able to generic trap any > error, and interpret it. > > I read 37.7.5. Trapping Errors, but the syntax requires to write explicitly > the exception condition, and not a generic one. > > Is it possible to build a generic trap or do you know a workaround for that > ? Won't OTHERS catch most of what you want? From the 8.2 docs: "The special condition name OTHERS matches every error type except QUERY_CANCELED. (It is possible, but often unwise, to trap QUERY_CANCELED by name.)"
On Thu, 15 Nov 2007, Sabin Coanda wrote: > > > > I read 37.7.5. Trapping Errors, but the syntax requires to write > > explicitly the exception condition, and not a generic one. > > > > Is it possible to build a generic trap or do you know a workaround for > > that ? > > > Sorry, I found the OTHERS condition that trap any error. > > But the question still remains to find how could I interpret it and found > the error code and message ? I think you'll want to look at SQLSTATE and SQLERRM inside the exception handler.
I think you should carefully read what the manual tells about PL/pgSQL (http://www.postgresql.org/docs/8.2/static/plpgsql.html) and especially about Trapping Errors (http://www.postgresql.org/docs/8.2/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING).
>>> Stephan Szabo <sszabo@megazone.bigpanda.com> 2007-11-15 19:36 >>>
On Thu, 15 Nov 2007, Sabin Coanda wrote:
> >
> > I read 37.7.5. Trapping Errors, but the syntax requires to write
> > explicitly the exception condition, and not a generic one.
> >
> > Is it possible to build a generic trap or do you know a workaround for
> > that ?
> >
> Sorry, I found the OTHERS condition that trap any error.
>
> But the question still remains to find how could I interpret it and found
> the error code and message ?
I think you'll want to look at SQLSTATE and SQLERRM inside the exception
handler.
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?
http://archives.postgresql.org