• 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

another "Exception" problem?I dont' know ?

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic