aspose file tools*
The moose likes Java in General and the fly likes Java Exception Handling regarding Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Exception Handling regarding Constructor" Watch "Java Exception Handling regarding Constructor" New topic
Author

Java Exception Handling regarding Constructor

DeepakDev Kumar
Greenhorn

Joined: Mar 06, 2013
Posts: 3
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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


Joanne
DeepakDev Kumar
Greenhorn

Joined: Mar 06, 2013
Posts: 3
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

Joined: Aug 05, 2005
Posts: 3169
    
  10
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

Joined: May 03, 2008
Posts: 4523
    
    5

DeepakDev Kumar, please BeForthrightWhenCrossPostingToOtherSites <- link
http://www.java-forums.org/new-java/69812-java-exception-handling-regarding-constructor.html


luck, db
There are no new questions, but there may be new answers.
DeepakDev Kumar
Greenhorn

Joined: Mar 06, 2013
Posts: 3
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

Joined: Oct 13, 2005
Posts: 36486
    
  16
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”.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Exception Handling regarding Constructor
 
Similar Threads
Generics - > Class Type Parameter Question
constructor
compile error -- Exception
Which of the Following is True?
Exception problem