• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can constructor throw exceptions

 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if yes

Then why i am getting error is here in this code


public class Test629 extends A
{
public static void main(String args[]) throws Exception{
Test629 t = new Test629();
}
}
class A
{
A() throws Exception
{
System.out.println("A Class");
}
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,
you are right that a constructor can throw an exception...but any Exception that is thrown has to be caught.

but in your example..... u are calling a Test629 classes default constructor which inturn calls the overloaded constructor of class A,

And You know that the default constructor of class Test629 does not handle the Exception thrown by the overloaded constructor of class A, thus you are getting an error saying "the thrown Exception has to be caught or rethrown".
if u now change your code to this.....(u will not get any error)
public class Test629 extends A
{
Test629() throws Exception
{
System.out.println("B Class");
}

public static void main(String args[]) throws Exception{
Test629 t = new Test629();
}
}
class A
{
A() throws Exception
{
System.out.println("A Class");
}
}
 
Karu Raj
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot shilpa

But i have one doubt

If the super class constructor throws exception then the subclass should also throw the exception.
 
Shilpa Asuthkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not necessay....
it can even handle the exception in a try catch block
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shilpa,

But then how can you actually catch the exception. look at my version of code.


class MyException extends Exception
{
}

class Test{
Test() throws MyException
{}

Test(int a){}
}

public class TechnoSample extends Test
{
TechnoSample()
{

}

public static void main(String args[])
{
System.out.println("hello world");
try{
TechnoSample t = new TechnoSample();
}
catch(MyException m){}
catch(Exception e){}

}
}


Here parent class Test has a contructor which throws a checked exception. Now the child class TechnoSample constructor does not throw it. I tried it and compiler yelled, exception must be caught.
You know this is because of in

TechnoSample(){
// implicit call to super();
}

now this super() call has something to do with exception. if try it manually
like,
TechnoSample(){
try{super();}catch(Exception e){}
}

But still Mr. Compiler is not very happy. he wants that super() must be the first line of code.

So the only way to make this code compiled, is to make child class constructor throws exception as well.
 
Shilpa Asuthkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes u r right
the compiler does complain about it......
i actually meant by handling the exception from the place where the subclass constructor is being called... n that was from main()--> where kartik instantiated the Test269 class.....
But Dilip i clearly wrote in the reply that.. becoz the default constructor of Test269 is internally calling the overloaded (no args) construtor of class A thats why it has to throw an exception as well
but a constructor which takes an arg as int need not throw Exception at all as it is not involved in the whole process( i mean it does not call the no args constructor of super class which throws an Exception)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic