• 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

Java Exception Handling regarding Constructor

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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”.
 
reply
    Bookmark Topic Watch Topic
  • New Topic