Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Sunday, September 2, 2012

CJ-23 : Run-time error in Java

Type the programs in NOTEPAD or DOS prompt itself, for Consol Application.
Type in NETBEANS or ECLIPSE for Window Application.
Here after , I am just typing the programs only..to know more about it , refer

Introduction to JAVA 

before doing any JAVA Programs.
***********************************************************************************************

import java.io.*;
public class error
{
    public static void main(String args[])
    {
            int a[]={5,10};
            int b=5;
            try
            {
                int x=a[2]/b-a[1];
            }
            catch(ArithmeticException e)
            {
                System.out.println("Division by zero");
            }
            catch(ArrayIndexOutOfBoundsException e)
            {
               System.out.println("Array Index error"); 
            }
            catch(ArrayStoreException e)
            {
                System.out.println("Wrong data type");   
            }
            
            int y=a[1]/a[0];
            System.out.println("y ="+y);  
     }
    }

=========================================================================
Output : 

Array Index error
y =2
========================================================================
Note
Given that,  int a[]={5,10}; 
That is , a[0]=5 and a[1]=10.

According to the formula given in the program , int x=a[2]/b-a[1];
But, there is no element in a[2].
So, the run-time error occured, and the println statement Array Index error displayed.

Then the program will end there and begin with the next catch statement  int y=a[1]/a[0];
That is , a[0]=5 and a[1]=10. And the result is 2.
========================================================================

No comments: