Skip to main content

Posts

Showing posts from February, 2009

Runtime Error (Exception) Handling

ADA handles programmer defined exceptions and system defined exceptionsSome of the system defined exceptions are like  NUMERIC_ERROR - raised whenever the abnormal precision in the program like divide by zero STORAGE-ERROR - raised whenever the program runs out of memory spaceCONSTRAINT_ERROR - asserts when a variable goes out of its boundTASKING_ERROR - raised during the incorrect use of tasksPROGRAM_ERROR- raised whenever an exception is not captured by any other conditionsExample on Exceptions
//Program to create two programmer defined exceptions
declare  P,PRESSURE:float;
HIGH_PRESSURE, LOW_PRESSURE:exception;
begin   loop     P:=READ_PRESSURE(PRESSURE);     if P<100> raise LOW_PRESSURE;
    elseif P>150 then  raise HIGH_PRESSURE;
end if;
end loop;
exception  when LOW_PRESSURE => put("Warning: Very Low Pressure");
when HIGH_PRESSURE => put ("Warning: High Pressure");

When exceptions are raised in multiple procedures or block and Procedures, handling the exceptions…

Packages in ADA

Benefits of PackagesPackages can be compiled, debugged independentlysoftware maintenance is very easierOnce package is developed, they can be made accessible to everyone who has access to it.packages provide more security                                                                                                            Packages contains two parts 1) a specification and 2) a body Specification interfaces to the outside world. body contains the executable statements associated with the functions
Examples of packages includ, COMPLEX NUMBERS Calculations, Creation of TIMER and CLOCK using packages and made as a library
Example //Package specification describes the functions and variables relevant to complex number and their addition and subtraction operations
package COMPLEX_NUMBER is type COMPLEX is  record  real, imag:float; end record; function ADD(a,b:COMPLEX) return COMPLEX; function SUB(a,b:COMPLEX) return COMPLEX; end COMPLEX_NUMBER;
//Paackage body will implement the function specified …

Blocks and Procedures in ADA

BlocksA Block in ADA contains two parts specification and a body. The specification specifies the variable used inside the block and the body executes the statement for the block
syntax block begin --- --- end
The main purpose of blocks are information hiding.
A Variable declared inside a block is accessible only within the block and nowhere outside the block.Example var x,y:integer begin    y:=0;  for x in 1..10 loop   block    var x:integer;     begin      for x in 1..5 loop     y:=y+x;    end loop; end; end loop;
(NB: the above example shows two variables x within the block and outside the block, The x inside the block is accessible only within that block)
Procedures and Functions Blocks are repeated whenever they are needed, but functions can be written once, called anytimesExamples //to define a function to show the smaller among two integers function SMALLER (A,B: integer) return integer is  begin if A < B then
return A; else
return B;
end if;
end SMALLER;
//function calling SMALLER D:=C+SMALLER(x,y);

ADA - Data types

Simple Data types
//creates three variables for TEMPERATURE which is of type float
type TEMPERATURE is new float;ta, tb, tc: TEMPERATURE;

//creates three variables for PRESSURE which is of type float
type PRESSURE is new float;pa, pb, pc: PRESSURE;
ta=pa is illegal as it is implicit type conversion
ta= TEMPERATURE(pa);is legal

//creates range between o to 10000
type HEIGHT is new float range 0..100000;

Sub types
type TIME is new float range 0..23.59;subtype MORNING is TIME range 0..11.59;subtype NOON is TIME range 12..16.00;subtype EVENING is TIME range 16.01..18.59

Enumerationtype
MONTH is (jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec);

floating point

type var1 is digits 8 range -1e20..1e20;
type var2 is digits 12 range -1e20..1e20;

fixed point

type var3 is delta 0.001 range 0.000..0.999;

Arrays
//SMALLINT is a array variable can have the range between 0 and 20, 10 such variables are declared

type SMALLINT is integer range 0..20;
type VAR4 is array(1..10) of SMALLINT;




ADA - programming language Features

Object oriented programming Strong typingGeneric programming/templates Exception handling Facilities for modular organization of code Standard libraries for I/O, string handling, numeric computing Systems programming Concurrent programming Real-time programming
Helps for periodic and event driven tasksAvoids unbounded priority inversionEfficient implementation of Priority ceiling in ADA
Distributed systems programming Interfaces to other languages (C, COBOL, Fortran)