12.4k views
5 votes
Which line(s) of code have a runtime or compile error:

package cop3337.exam3;
public class COP3337Exam3
public static void main(String[] args)
int[] numbers = new int[10];
try
try
2
3
for(int i=0; i< 10; i++) numbers [i] = 18 *i;
13
14
if(i%2== 1) throw new Exception("P1");
15 16
17
if(i%2==0)
throw new ArrayIndexOutOfBoundsException("P2");

18
19
20
//end for
catch(ArithmeticException e)
System.out.print(e.getMessage() + " L1"); catch(ArrayIndexOutOfBoundsException e)
System.out.print(e.getMessage()+" L2 "); catch(Exception e)
System.out.println(e.getMessage()+" L3 "); finally
throws new Exception("L4");
catch(Exception e)
33
34
35
System.out.println(e.getMessage()+" L5");
36
37
System.out.print("\\");
//end main
38
39
//end class
12, 32
15
No error in code:
29

User Nataliia
by
7.8k points

1 Answer

4 votes

The lines of code that have runtime or compile errors are:

12: The line is missing a semicolon to terminate the line.

32: The line `throws new Exception("L4");` is syntactically incorrect. The `throws` keyword is used in a method declaration to specify the exceptions that can be thrown by that method, but it is not used within a `finally` block.

Additionally, there is an error in the code logic:

15: The `throw new Exception("P1");` statement is inside the `for` loop without any condition. It will throw an exception in every iteration of the loop.

So, the lines with errors are:

12

15

32

The line 29 states that there are no errors in the code, but that is incorrect.

User Ramin
by
8.0k points

Related questions

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.