Subject Re: [firebird-support] how can i handle the exception 'object' in a general way
Author Helen Borrie
At 08:44 AM 9/02/2005 +0000, you wrote:


>Hi,
>
>I created some custom exceptions, how can i handle the
>exception 'object' in a general way in the 'when exception block'?
>
>WHEN EXCEPTION Custom_Exception1, EXCEPTION Custom_Exception2 DO
> BEGIN
> MyException = EXCEPTION.RDB$EXCEPTION_NAME;
> MyExceptionText = EXCEPTION.RDB$MESSAGE;
> END

You should probably get hold of the IB6 DataDef.pdf and look at the
examples there.

In order to cause a custom exception, you have to make that exception occur
in your code:

BEGIN
...
BEGIN
..........
If (<something_you_dislke>) then
exception Custom_Exception1;
if (<something_else_horrible>) then
exception Custom_Exception2;
.........
END

Then handle exceptions in the order you want them handled - typically,
starting with the most specific and working down to the most generic:

WHEN Custom_Exception1 do
BEGIN
<do something to handle the exception>
END
WHEN Custom_Exception2 do
BEGIN
<do something to handle thIs exception>
END
WHEN ANY do
BEGIN
<do something to handle any other exception>
END
/* --------------------- */
/* Now carry on */
END

You can nest exception blocks outwards - if a handler isn't found at the
current level, the engine will keep working its way outwards, through the
BEGIN...END blocks in the main execution flow, until it finds a suitable
handler. If it reaches the final END statement and it hasn't found a
handler that fits the exception that was thrown, it returns control to the
client, returning that exception.

Note, you can also have handlers listening for specific SQLCODE errors (a
negative 3-digit number, e.g. -902) or gdscode errors (which are symbols,
like e.g. arith_except, which is the symbol for gdscode 335544321, with the
message "Arithmetic exception, numeric overflow, or string truncation").

You should only write handlers for things you actually want to handle. If
you want to return the exception, let the engine take care of it.

You might also want to use a handler to assign some specific text to a
custom exception, before re-raising it. You can read about this in the
Firebird 1.5 release notes...

./heLen