• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

another "Exception" problem?I dont' know ?

 
Tu Ran
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {
A() throws Exception {
System.out.println ("Executing class A constructor");
throw new IOException(); //error? why?
}
}
class B extends A {
B() {
System.out.println ("Executing class B constructor");
}
public static void main ( String args[] ) {
try {
A a = new B();
} catch ( Exception e) {
System.out.println( e.getMessage() );
}
}
} //what's wrong with the codes? and how can you correct it?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the compiler say?

maybe you should add an import java.io.*; statement on the first line since only java.lang.*; is automatically imported by the compiler.
[ March 05, 2002: Message edited by: Valentin Crettaz ]
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
maybe you should add an import java.io.*; statement on the first line since only java.lang.*; is automatically imported by the compiler.
[ March 05, 2002: Message edited by: Valentin Crettaz ]

Hi Valentin,
I tried to add the import statement as you said. Then I got another compile error saying "unreported exception java.lang.Exception; must be caught or declared to be thrown." I understand the fix is to add "throws Exception" at constructor for B class. I just wonder why this error occurs. Is it because B constructor implicitly calls A constructor? Or is it because B constructor inherits A constructor so it has to have throws as in A constructor?
Thanks a lot,
Jenny
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jenny,
constructors are not inherited.
B's constructor will invoke A's constructor and since the latter may throw an Exception, the exception has to be caught by B's constructor somewhere or declared to be thrown further.
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
Jenny,
constructors are not inherited.
B's constructor will invoke A's constructor and since the latter may throw an Exception, the exception has to be caught by B's constructor somewhere or declared to be thrown further.

Thank you, Valentin for your reply. Can I ask a further question? When you say B's constructor invokes A's constructor, do you mean Java compiler automatically adds super() statement in the beginning of B's constructor even if we don't specify it?
Thanks,
Jenny
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right Jenny, if you don't explicitely specify it, the compiler takes care of it.
When you write this:

The compiler actually augments it to:

(Result obtained with javap)
 
Ratna Singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
Jenny,
constructors are not inherited.
B's constructor will invoke A's constructor and since the latter may throw an Exception, the exception has to be caught by B's constructor somewhere or declared to be thrown further.

Hello ! I found the above information great. I was getting the same IOException compiler error that got me looking the forum.
However I am having trouble implementing a derived class constructor doing a try-catch. The derived ctor
implemented as :
B() throws Exception {
etc ...
} works fine.
B() {
try {
etc ..
} catch ( Exception p )
gives a compiler error :
B.java:11: unreported exception java.lang.Exception; must be caught or declared
to be thrown
B() {
^
1 error
Executing class A constructor

How should a derived ctor be implemented that does not propagate exceptions ?
Thanks,
Ratna.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem in your code is that the Exception that may be thrown from the superclass constructor actually happens before the control enters the try block because before executing the body of constructor B() a invocation to super() is performed. The Exception happens at that level, that is:

Besides since super() must be the first statement in the constructor's body, you have to throw the Exception further and catch it in the method that invoked the constructor.
This all makes sense if you realize the following:
If the superclass constructor throws an Exception is actually means that the superclass has not been instantiated, and thus, the subclass cannot be instantiated correctly. Thus the whole object creation must fail and that must be reported in the method that tried to create the object. Why would the object itself care about the fact that it has not been instantiated? The caller method cares, the object does not.
 
Ratna Singh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
This all makes sense if you realize the following:
Why would the object itself care about the fact that it has not been instantiated? The caller method cares, the object does not.

Yep, thanks for the explanation.
Ratna
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic