Runtime Error (Exception) Handling

ADA handles programmer defined exceptions and system defined exceptions

Some of the system defined exceptions are like 
  1. NUMERIC_ERROR – raised whenever the abnormal precision in the program like divide by zero 
  2. STORAGE-ERROR – raised whenever the program runs out of memory space
  3. CONSTRAINT_ERROR – asserts when a variable goes out of its bound
  4. TASKING_ERROR – raised during the incorrect use of tasks
  5. PROGRAM_ERROR- raised whenever an exception is not captured by any other conditions
Example on Exceptions

//Program to create two programmer defined exceptions

    if P
    elseif P>150 then 
end if;
end loop;

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 can be done using any of the procedure 
  • For Example, If there are three procedures X,Y and Z. X calls Y and Y calls Z, now there is an exception at Z (which does not have exception handler), the control will transfer the previous procedure Y (Which contains the exception handling mechanism) and handled over there.
  • Similary if a block does not have an exception handler, then the procedure which includes the block handles the expection , if that procedure contains the exception handler

Packages in ADA

Benefits of Packages

  1. Packages can be compiled, debugged independently
  2. software maintenance is very easier
  3. Once package is developed, they can be made accessible to everyone who has access to it.
  4. 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
//Package specification describes the functions and variables relevant to complex number and their addition and subtraction operations

type COMPLEX is 
 real, imag:float;
end record;
function ADD(a,b:COMPLEX) return COMPLEX;
function SUB(a,b:COMPLEX) return COMPLEX;

//Paackage body will implement the function specified in the package specification

package body  COMPLEX_NUMBER is
with ROOTS; //ROOTS is also a package and now used as alibrary
use ROOTS;

function ADD(a,b:COMPLEX) return COMPLEX is 
end ADD;

function SUB(a,b:COMPLEX) return COMPLEX is 
end SUB;


//Package COMPLEX_NUMBER made available as a library

with COMPLEX_NUMBER;  //retrieve the COMPLEX_NUMBER package
package CMPLXUSE is 
use COMPLEX_NUMBER; //make package available to the procedure

Blocks and Procedures in ADA


A 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

  • 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.
var x,y:integer
 for x in 1..10 loop
   var x:integer;
    for x in 1..5 loop
   end loop;
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
  1. Blocks are repeated whenever they are needed, but functions can be written once, called anytimes
//to define a function to show the smaller among two integers
function SMALLER (A,B: integer) return integer is 
if A < B then
  return A;
return B;
end if;
//function calling SMALLER

SunSPOT(Sun Small Programmable Object Technology)

SunSPOTs are embedded hardware modules that are equipped with a 180 MHz CPU, 512 KB RAM, 4MB flash memory, three on-board sensors (temperature, light and three-axis accelerometer), hardware interfaces for the integration of arbitrary external sensors, and the IEEE 802.15.4 wireless transmission technology, which is enabled for mesh networking. SunSPOTs are entirely programmable in the Java programming language and thus help to abstract from the underlying hardware. No direct interaction by machine code with the hardware is necessary, which significantly eases the development of sensor based applications.

The SunSPOT sensor technology was developed primarily to satisfy three target groups: education (introducing pupils and students to related topics

such as programming, networking, embedded systems, robotics or hardware), research and development (allowing for fast implementation ofsystems that integrate sensor technology and for easy testing of their behavior), and hobbyists (developing small sensor based applications for personal needs).

One of the major design goals of SunSPOTs was to provide a tool for rapidly prototyping sensor-based applications, and for testing and verifying algorithms on a small scale prior to deploying them in industrial operation This tool can be exploited to reduce costs by evaluating novel algorithms a priori before adapting them to real world problems.

This form of sensor technology will prove useful in a broad spectrum of applications and domains, such as

  • early detection and warning systems,
  • environmental monitoring,
  • automotive engineering,
  • warehouse/container management,
  • logistics,
  • monitoring of buildings,
  • home automation,
  • weather forecasting,
  • medical monitoring of patients and diagnosis,
  • agriculture and farming.


SunSPOT Processor Board

* 180 MHz 32 bit ARM920T core – 512K RAM/4M Flash
* 2.4 GHz IEEE 802.15.4 radio with integrated antenna
* USB interface
* 3.7V rechargeable 720 mAh lithium-ion battery
* 32 uA deep sleep mode

General Purpose Sensor Board

* 2G/6G 3-axis accelerometer
* Temperature sensor
* Light sensor
* 8 tri-color LEDs
* 6 analog inputs
* 2 momentary switches
* 5 general purpose I/O pins and 4 high current output pins

Squawk Virtual Machine

* Fully capable J2ME CLDC 1.1 Java VM with OS functionality
* VM executes directly out of flash memory
* Device drivers written in Java
* Automatic battery management

Developer Tools

* Use standard IDEs. e.g. NetBeans, to create Java code
* Integrates with J2SE applications
* Sun SPOT wired via USB to a computer acts as a base-station

For more details, visit the homepage here

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

Enumeration type
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;

//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 typing
  • Generic 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
  1. Helps for periodic and event driven tasks
  2. Avoids unbounded priority inversion
  3. Efficient implementation of Priority ceiling in ADA
  • Distributed systems programming
  • Interfaces to other languages (C, COBOL, Fortran)