Previous topic | Ada Home Page | Index

User defined exception

Package specification

    package TRIGONOMETRY is
        function SIN (X : FLOAT) return FLOAT;
        function COS (X : FLOAT) return FLOAT;
        function TAN (X : FLOAT) return FLOAT;
        TAN_ERROR : exception;
    end TRIGONOMETRY;

Package body

    package body TRIGONOMETRY is

        function SIN (X : FLOAT) return FLOAT is
        begin
            ...
        end SIN;

        function COS (X : FLOAT) return FLOAT is
        begin
            ...
        end COS;

        function TAN (X : FLOAT) return FLOAT is
        begin
            return SIN(X) / COS(X);
        exception
            when NUMERIC_ERROR => raise TAN_ERROR;
        end TAN;

    end TRIGONOMETRY;

User program

    with TEXT_IO, TRIGONOMETRY;
    procedure compute_tan is

        package flt_io is new TEXT_IO.FLOAT_IO(FLOAT);
        use TEXT_IO, flt_io, TRIGONOMETRY;

        number, res : FLOAT;

    begin -- compute_tan
        loop
            begin

                PUT ("Enter a real number (or CTRL-D to quit)");
                exit when END_OF_FILE;
                GET (number); SKIP_LINE;
                res := tan(number);
                PUT("Tangent is "); PUT(res); NEW_LINE;

            exception
                when TAN_ERROR =>
                    PUT_LINE ("Tangent is too big");

            end;
        end loop;
    end compute_tan;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 26 Feb 96