SCJP Module-6 Question-17


 

SCJP Module-6 Question-17

The sample program given below will check your understanding about the Exception and their Handling Java. It is very helpful for successful completion of SCJP examination.

The sample program given below will check your understanding about the Exception and their Handling Java. It is very helpful for successful completion of SCJP examination.

Given below the sample code :

1 public class A {
2 static void test() throws Error{
3 if (true) throw new AssertionError();
4 System.out.print("test ");
5 }
6 public static void main(String[] args) {
7 try { test(); }
8 catch (Exception ex) { System.out.print("exception "); }
9 System.out.print("end ");
11}}

How can we correct the above code ?

1. No need of correction

2. By changing "Exception" class to "Error" at line 8 in "caught"

3. By changing "throws Error" to "throws Exception" at line number 2.

4. By throwing exception in place of error at line 2.

Answer :

(2)

Explanation :

Above code will give uncaught exception as output. Since it is of type "error " so it should be handled by error class object.

Ads