wellnwill Marathahalli Bangalore

Constructor and Exception Handling

Suppose super class constructor is throwing any checked exception then child class constructor must have to throw the same checked exception or its parent exception.

import java.io.*;

class Bulb{
          
       Bulb() throws IOException
       {
        
       }
}
class Surya extends Bulb{

      Surya()
       {
        
       }
}
class Test166
{
      public static void main(String args[])
      {	
      System.out.println("inside main()");
      }
}

//O/P-CTE-unreported exception IOException:must be caught or declared to be thrown.

import java.io.*;

class Bulb{
          
       Bulb() throws IOException
       {
        
       }
}
class Surya extends Bulb{

      Surya() throws IOException
       {
        
       }
}
class Test166
{
      public static void main(String args[])
	  {	
      System.out.println("inside main()");
      }
}
//O/P-inside main()

import java.io.*;
class Bulb{
      Bulb() throws IOException
       {
       }
	   }
class Surya extends Bulb
{
        Surya() throws Exception//Parent type is allowed
        {
        }
		}
class ExcepTest6
{
      public static void main(String args[])
	  {	
      System.out.println("inside main()");
      }
}
//CTS
Footer with Map

Top