Sunday, December 09, 2012

Exceptions, Again

Copyright 2012 by Shawn H Corey. Some rights reserved.
Licence under CC BY-SA 3.0

How exceptions are handled needs some clarification.

Exeption Handling

The only groups allowed are any for any exception. If used, no other exception handling is allowed for the statement. The other grouping is other, to group exceptions not yet handled. This handler must be the last for the statement.

There are only three methods of handling exceptions.

Ignore it.

The exception can be ignore. When it happens, the program does nothing in response.

Example:

    when exception
        ignore
Declare another.

When an exception happens, another is declared, that is, another is thrown to the caller. This thrown one must be part of the subroutines interface.

Example:

    when exception
        declare another exception
Set a variable to a fixed value.

A variable can be set to a fixed value, that is, a constant or a configuration variable.

Example:

    when exception
        variable ← 0

Trend Module's slope Function

Another look at the slope function:

    module Trend

    function Number slope
        given
            Point 1st
            Point 2nd
        returns
            Number slope
        except
            when infinite slope
            when points too close to determine slope

    begin

        Boolean overflowed    ← FALSE
        Boolean X underflowed ← FALSE
        Boolean Y underflowed ← FALSE

        Number Δy ← 2nd.y - 1st.y
            when overflow
                overflowed ← TRUE
            when underflow
                Y underflowed ← TRUE

        Number Δx ← 2nd.x - 1st.x
            when overflow
                overflowed ← TRUE
            when underflow
                X underflowed ← TRUE

        if X underflowed and Y underflowed
            declare points too close to determine slope

        if X underflowed
            Δx ← 0

        if Y underflowed
            Δy ← 0

        if overflowed
            Δy ← 2nd.y ÷ 2 - 1st.y ÷ 2
                when any
                    Δy ← 0

            Δx ← 2nd.x ÷ 2 - 1st.x ÷ 2
                when any
                    Δx ← 0

        slope ← Δy ÷ Δx
            when divide by zero
                declare infinite slope
            when overflow
                declare infinite slope
            when underflow
                slope ← 0

    return slope