Subject Re: Division by zero chech
Author Adam
--- In firebird-support@yahoogroups.com, "majstoru" <majstoru@y...> wrote:
>
> Hi,
>
> Does Firebird have a solution to check divizion by zero error?

Firebird has an exception that is raised when you do a "arithmetic
exception, numeric overflow, or string truncation", and dividing by 0
is one way to raise this.

> I have SP that update one table with some math operation with data
> from another table, when source table still have a records which will
> be generated divizion by zero, I cna't check this by SP code!

There are a number of ways to check this. The most boring way is using
an IF statement to avoid the problem.

IF (:DENOMINATOR <> 0) THEN
BEGIN
SOMEVALUE = :NUMERATOR / :DENOMINATOR;
END
ELSE
BEGIN
SOMEVALUE = NULL;
END

boring, but effective.

If you want to deal with it like a true programmer, then catch the
exception. (hint: you need to do some reading on the WHEN EXCEPTION
??? DO block)

BEGIN
SOMEVALUE = :NUMERATOR / :DENOMINATOR;

WHEN ANY DO
BEGIN
SOMEVALUE = NULL;
END
END

Of course you wouldn't use ANY here, quote the exact exception using
one of the methods (GDSCODE / SQLCODE / Exception Name) in LangRef.pdf

If you do not catch (or avoid) the exception in the first place, then
your entire stored procedure is reversed out. This is correct
behaviour in keeping with atomicity rules! Either the entire procedure
completely succeeds, or none of it does.

This behaviour can be used to do consistency checking of the data
inside triggers (if some consistency rule is broken, then you can
raise a custom exception and the insert / update / delete operation
will fail.

Adam