Share on Google+Share on Google+

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.

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 ");

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 :


Explanation :

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


Posted on: July 14, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.