Subject | Re: Division by zero chech |
---|---|
Author | majstoru |
Post date | 2005-12-29T09:50:08Z |
--- In firebird-support@yahoogroups.com, "Adam" <s3057043@y...> wrote:
Sasa
>wrote:
> --- In firebird-support@yahoogroups.com, "majstoru" <majstoru@y...>
> >0
> > 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
> is one way to raise this.will
>
> > I have SP that update one table with some math operation with data
> > from another table, when source table still have a records which
> > be generated divizion by zero, I cna't check this by SP code!using
>
> There are a number of ways to check this. The most boring way is
> an IF statement to avoid the problem.pdf
>
> 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.
>then
> If you do not catch (or avoid) the exception in the first place,
> your entire stored procedure is reversed out. This is correctprocedure
> behaviour in keeping with atomicity rules! Either the entire
> completely succeeds, or none of it does.Thats it, thanks Adam!
>
> 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
>
Sasa