Share on Google+Share on Google+

SCJP Module-6 Question-4

Advertisement
A sample program given below which tests your exception handling concepts of Java.

Given below the sample code :

1   import java.io.StreamCorruptedException;
2   import java.net.MalformedURLException;
3   public class SuperClass {
4   public static void main(String[] args) {
5   try {
6   throw new MalformedURLException("error in url");
7   throw new StreamCorruptedException("error in file");
8   throw new Exception("erorr");
9   System.out.println("try block ");
10  } catch (MalformedURLException e) {
11  System.out.println("error in URL");
12  } catch (StreamCorruptedException e) {
13  System.out.println("error in file contents");
14  } catch (Exception e) {
15  System.out.println("exception");
16  } finally {
17  System.out.println("in finally block");
18  }
19  System.out.println("success");
20  }}

What will be the output of the following code?

1. try block
    error in URL
    success
2. try block
    exception
    success
    in finally block
3. try block
    in finally block
    success
4. compilation error

Answer :

4

Explanation :

Unreachable code at line 7,8 & 9.
Unreachable catch block for "StreamCorruptedException". This exception is never thrown from the try statement body.

Advertisements

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.