22.9k views
3 votes
When the code below is executed how many exceptions are thrown:

package cop3337.exam3;
public class COP3337Exam3
public static void main(String[] args)
int[] numbers = new int[10];
try
try
for (int i=0; i< 10; i++) numbers[i] = 10*1;
if (182 == 1)
throw new Exception("P1");
if (1%2==0)
throw new ArrayIndexOutOfBoundsException("P2");
//end for
catch(ArithmeticException e)
System.out.print(e.getMessage() + " L1");
catch(ArrayIndexOutOfBoundsException e) System.out.print(e.getMessage() + " 12");
catch(Exception e) System.out.println(e.getMessage() + " L3 ");
finally throw new Exception("L4");
catch(Exception e)
System.out.println(e.getMessage()+" L5 ");
System.out.print("\\");
//end main //end class
17
0
1
2
3

1 Answer

4 votes

Answer:

2

Step-by-step explanation:

When the code is executed, a total of 2 exceptions are thrown.

The exceptions thrown are:

1. ArrayIndexOutOfBoundsException: This exception is thrown on line 14 when the condition `if (1%2==0)` evaluates to false. As a result, the `throw new ArrayIndexOutOfBoundsException("P2");` statement is executed.

2. Exception: This exception is thrown in the `finally` block on line 20. The `finally` block is always executed, regardless of whether an exception is thrown or not. In this case, the `throw new Exception("L4");` statement is executed, throwing the exception.

So, the correct answer is:

2

User Kmdsax
by
7.8k points

Related questions