Subject Re: [firebird-support] selectable SP, exception handling
Author Helen Borrie
At 08:49 AM 22/12/2009, you wrote:

> num = CAST('355a' as integer);
>
> IF (num > 0)
> then NR_NUM = num;
> else //repeat, whatever this needs more work
>
> when any do begin
> exception ;
> end
>
> suspend; // this is where it stumbles
> end
>end
>
>The problem is that the parser says that suspend is "invalid token".

The parser is expecting the name of an exception that you have previously defined using CREATE EXCEPTION.

However, if I understand correctly what you want to do (skip over any record that throws an exception), then throwing another exception won't get you out of jail. If you want your exception handler to pick up all exceptions and discard them, provide an "empty" handler, e.g.

when any do begin
-- do nothing
end

After an exception has been handled this way, execution returns to the start of the same begin...end block in which the exception occurred. Unhandled exceptions simply drop through to the last 'end' statement and return control to the caller. So design your looping blocks and place your exception handling blocks so that execution will return to the right place and keep executing.

Usually, you would only want to throw a specific exception where you want specific handling, e.g.,

for....do
begin
...
if (some condition) then
exception MyException;
.... (do more stuff)
suspend; -- only happens if no exception occurred
end
when exception MyException do
begin
.... (do something such as write to a log)
end
when any do -- now deal with any other exceptions
begin
-- do nothing (swallow the exception and move on to the next iteration)
end

end

Don't take these examples as a definitive tutorial, "Everything you need to know about PSQL logic"! But they might help you to see how exception handling can be used to create "savepoint logic" inside your modules.

Also, be aware that advancing versions of Firebird have provided enhancements to exception handling logic. Particularly, look in the PSQL section of Firebird release notes, starting with the Fb 1.5.6 notes and tracking through to the Fb 2.x notes. Better still, the Language Reference Update guides have them nicely potted up for you, with examples.

./heLen