info@meritcampus.com    +91-85006-22255
...
`

Exception Handling2840
What will be the output of the following program?
public class FirstException {
public static void main(String args[]) {
try {
int a[] = new int[5];
a[5] = 30 / 0;
}
catch (ArrayIndexOutOfBoundsException e) {
}
catch (ArithmeticException e) {
}
catch (Exception e) {
}
System.out.println("Remaining code");
}
}

Topic:

### User comments below.All of them might not be correct.

A

Posted by Ammad Islam    2015-02-20 09:31:02

Ans is C.
Here first the arithmetic exception will raise over the arrayindexoutofbounds exception, as first the right hand side of the assignment will execute... so we will get 2nd catch block executed followed by the code after the try-catch... So output will be
Remaining code

Posted by Gopinath Manchikanti    2015-02-20 10:14:27

E

Posted by Sahithi Boggarapu    2015-02-20 12:17:18

here option is C
execution starts from main() and enter into try block and a array was created with a name a[]
then a intsruction is
a[5]=30/0;
but here their is chance two expection were raised
1.   a[5] is given ArrayIndexoutofbound exception(because we created only five elements of array A[] but we accesing the 6th element thats its give an exception);
2.   30/0 means it give arthimetic expetion( beacuse in 30/0 means somenthing which divedes by zero(it means un defined)  system does not know what do thats why this situation raise to arthimetic expection);

from above 2 situation 2nd will be excuted first because excution is from right to left means first 30/0 and that result will stores in a[5];
thats why it gives arthimetic exception that will throw to a catch block which takes a argument as arthimetic expection object.
in this program 3 catch blocks

1. catch (ArrayIndexOutOfBoundsException e)
{
}
2.catch (ArithmeticException e)
{
}
3.
catch (Exception e)
{
}
System.out.println("Remaining code");

from this 3 blocks it will go to 2nd catch block and prints "Task1 is completed"
and it come out from catch block and go to last statement that is printing statement  and it prints"Remaining code"
and result is
Remaining code
and option is C

Posted by Goutham Singarapu    2015-02-21 16:03:44

Ans is c..here we have the Exceptions concept which will occur during the run time which vl cause the abnormal termination of the program..by using try-catch mechanisam we can handle the exceptions so can avoid abnormal termination..
Execution:
here we create the an array of int with the size 5 so indexes range from 0-4..next we are assigning the a[5]=30/0 here we have 2 exceptions occur 1st Right side exp vl evaluate so here arithmetic exception vl raise..so it wont go for assignment..from that try ArithmeticException class object is thrown than we are checking the catches in 1st catch wont handle in the next catch it will handle and prints task1 is completed so exception is handled so we avoided the abnormal termination so remaining code get executed

Posted by Uday Kumar    2015-02-24 02:04:09

This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-24 03:30:47