Share on Google+Share on Google+

SCJP Module-6 Question-10

Advertisement
The given sample program will test your understanding about the use of try, catch, finally in Java. This helps very much to clear your concepts about the exception handling.

Given below the sample code :

 1 public class SuperClass {
 2 static String str = "";
 3 public static void main(String[] args) {
 4 try {
 5 str += "2";
 6 throw new Exception();
 7 } catch (Exception e) { str += "3";
 8 } finally { str += "4"; interrupt(); str += "6";
 9 }
10 System.out.println(str);
11 }
12 static void interrupt() { int a = 0; int b = 8/a; }
13 }

How can we correct the above code :

1. No need of correction.

2. By placing the method "interrupt" inside "try" block.

3. By changing the value of variable 'a' to any number except zero.

4. By removing the "interrupt" method from this code.

Answer :

(2) or (3) or (4)

Explanation :

Error in this code is due to the ' / by zero ' operation of method "interrupt()".

Advertisements

Advertisement

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.