/* Program to demonstrates Nested try statements */
class ExceptionDemo5
{
public static void main(String args[ ])
{
try
{
int x = args.length;
System.out.println("Number of Command line arguments : "+x);
/* if no command line arguments are present,
the following statment will generate
a divide-by-zero exception */
int y = 500/x;
try
{ // nested try block
/* if one command line argument is used,
then a divide-by-zero exception
will be generated by the following code. */
if (x==1) x = x/(x-x); // division by zero
/* if two command line arguments are used,
then generate an out-of-bound exception */
if(x==2)
{
int arr[]={5,20,25};
arr[20]= 100; // generate an out-of-bounds exception
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n Array index out of Bounds...."+e);
}
}
catch(ArithmeticException e)
{
System.out.println("\n Division by zero....."+e);
}
}
}
class ExceptionDemo5
{
public static void main(String args[ ])
{
try
{
int x = args.length;
System.out.println("Number of Command line arguments : "+x);
/* if no command line arguments are present,
the following statment will generate
a divide-by-zero exception */
int y = 500/x;
try
{ // nested try block
/* if one command line argument is used,
then a divide-by-zero exception
will be generated by the following code. */
if (x==1) x = x/(x-x); // division by zero
/* if two command line arguments are used,
then generate an out-of-bound exception */
if(x==2)
{
int arr[]={5,20,25};
arr[20]= 100; // generate an out-of-bounds exception
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n Array index out of Bounds...."+e);
}
}
catch(ArithmeticException e)
{
System.out.println("\n Division by zero....."+e);
}
}
}
0 comments:
Post a Comment