Saturday, 25 May 2013

Exception - 13 -

What is the result from the following code when you run? 

package net.dharmaraj;

import java.io.*;

class A
{
    A() throws Exception
    {
        System.out.println("Executing class A constructor");
        throw new IOException("Dunu");
    }
}

public class B extends A
{
    B()
    {
        /*
         * try{ super(); System.out.println ("Executing class B constructor"); }catch(Exception e){}
         */
        System.out.println("Executing class B constructor");
    }

    public static void main(String args[])
    {
        try
        {
            A a = new B();
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}


A) Executing class A constructor 
B) Executing class B constructor 
C) Runtime error 
D) Compile time error



Answer -- d inside constructor we can not give try catch block because inside constructor super() or this() must be first statement implicitly or explicitly 


Compaile Error:--

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type Exception

at net.dharmaraj.B.<init>(B.java:16)
at net.dharmaraj.B.main(B.java:28)

No comments:

Post a Comment