• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Exception Handling regarding Constructor

 
DeepakDev Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody

In the below code ...If i remove constructor Test11(int i) then it compile and run.Can anybody clarify why compiler ask to include throws clause in constructor Test11(int i)...though I am explicitly calling Test11() by .... Test11 t = new Test11();


public class Test11 extends A{
Test11()throws Exception{
System.out.println("Test10 Class");
}
Test11(int i){}
public static void main(String args[]) throws Exception{

Test11 t = new Test11();
}


}
class A{
A() throws Exception{
System.out.println("A Class");
}
}

thanks in advance
Deepak
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the first thing that an empty constructor does ?
If you don't know you need to read this. Particularly the Note near the bottom
 
DeepakDev Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:What's the first thing that an empty constructor does ?
If you don't know you need to read this. Particularly the Note near the bottom


Dear Neal

Thanks for your reply.
Your provided link did not clear my doubts.

It is understandable that before sub-class constructor runs it will call super class constructor.
If I call Test11 t = new Test11(); then it will look Test11() and in Test11() the first statement will be super().

Then why compiler ask to throws exception with Test11(int i) like Test11(int i) throws Exception {}

cheers
Deepak
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DeepakDev Kumar wrote:If I call Test11 t = new Test11(); then it will look Test11() and in Test11() the first statement will be super().

Correct. So what constructor will that call to super() actually invoke ? Or to put it another way - why have you declared this constructor to throw an exception but you don't understand why you need to declare the second constructor to throw an exception.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DeepakDev Kumar, please BeForthrightWhenCrossPostingToOtherSites <- link
http://www.java-forums.org/new-java/69812-java-exception-handling-regarding-constructor.html
 
DeepakDev Kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
DeepakDev Kumar wrote:If I call Test11 t = new Test11(); then it will look Test11() and in Test11() the first statement will be super().

Correct. So what constructor will that call to super() actually invoke ? Or to put it another way - why have you declared this constructor to throw an exception but you don't understand why you need to declare the second constructor to throw an exception.


Thanks a lot Neal for clearing my doubt.

regards
DeepakDev
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button; I would have appleid it retrospectively but the code was not indented, so it would have had no effect. And please don’t say “Neal”. It’s “Ms Neal”.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic