Find error in a Java Program

Asked By 7060 points N/A Posted on -
qa-featured

The code given below is a Java code.

Find out that after the execution of the code the exception "array index out of bound" has occurred or not?

Also identify the mistakes in the program if any? And then write the correct program. ————————————————————— public class Excep { public static void main(String [] arr) { try { try { int array[]=new int[20]; array[100]=5; } finally { System.out.println("In Finally BLock.."); } System.out.println("Outside Finally Block.."); } catch(ArrayIndexOutOfBoundsException EX) { System.out.println("In Catch Block…"); } System.out.println("Exiting…"); } }

SHARE
Best Answer by jimhawkins89
Best Answer
Best Answer
Answered By 0 points N/A #79429

Find error in a Java Program

qa-featured

First of all the code has some errors like 

3) public static void main (string [] arr) > string args[]

20) System.out.println() > System.out.println()

After correcting the errors we do get the "array index out of bonds exception" as we have exceeded the maximum size of 20 , initialized to the array.

public class Excep

{
public static void main(String args[])
{
try
{
try
{
int array[]=new int[20];
array[100]=5;
}
finally
{
System.out.println("In Finally BLock..");
}
System.out.println("Outside Finally Block..");
}
catch(ArrayIndexOutOfBoundsException EX)
{
System.out.println("In Catch Block…");
}

System.out.println("Exiting…");
}
}

Answered By 5 points N/A #79430

Find error in a Java Program

qa-featured

Hi , User hope the following steps will guide you in fixing this issue.:

The runtime error you got "Array Index Out Of Bound" is an exception that is generally occurring at the runtime and the problem may be with the size of an array and output may be expected the finally block will definitely be executed and then the control transfer from try to catch if any exception arises and it is continuing to throw a block to throw the object caught by the catch block.
 
The reason may also be the installation of Java may not be done correctly so it's better to check and run the application with clear installation and If you have installed correctly then the next step if you get the error is to update to be done by using the Java official website "www.javasoft.com". 
 
Even if the 2 steps also  fails to fix your issue then open the DOS prompt by moving to the folder with VUE and then running the 
Command: "Java -jar VUE. jar".   
 
Thank you.
 
Answered By 0 points N/A #196098

Find error in a Java Program

qa-featured

Hi

Please find the below code without any error.
 
public class Excep {
 public static void main(String[] arr) {
 try { 
try {
int array[]=new int[20];
array[100]=5;
} finally {
System.out.println("In Finally BLock..");
} 
System.out.println("Outside Finally Block.."); 
 } catch(ArrayIndexOutOfBoundsException EX) {
System.out.println("In Catch Block…"); 
 }
 System.out.println("Exiting…");
 }
}
 
This will be the output of the above program.
 
In Finally BLock..
In Catch Block…
Exiting…
 
ArrayIndexOutOfBoundsException has occurred since the array index  went out of bound.
 
Hope it helps.

Related Questions