7
Choosing a Handler
An unhandled error is sent one level up
into the outer PL/pgSQL block, if available
into the calling routine, if available
The search path of a handler is determined by the call stack
i.e., it is not defined statically, it depends on the program execution
An unhandled error is passed to the client
the transaction enters the failure mode and has to be rolled back by the client
the error is registered in the server log
If none of the conditions listed in the EXCEPTION section is triggered, the
error goes one level up.
If an error has occurred in the inner block of a nested structure, the server
will search for a handler in the outer block. If there is no suitable handler
either, the whole outer block will be treated as failed, while the error will be
passed to the next nesting level, and so on.
If we have gone through the whole nested structure and have not found an
appropriate error handler, the error goes further up to the level of the routine
that has called this block. So you have to analyze the call stack to
determine the order in which different error handlers will be tried.
If none of the available error handlers is triggered:
- the error message usually gets into the server log (the exact behavior
depends on the server settings; see lecture “PL/pgSQL. Debugging”);
- the error is reported to the client that has initiated this operation in the
database. The client has to face the fact: the transaction enters the failure
mode, and it can only be rolled back.
It is up to the client to choose how to handle the error. For example, psql will
display the error message and all the debugging information available. An
end-user client may display a classic message like “contact your system
administrator”.