File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "another "Exception" problem?I dont Watch "another "Exception" problem?I dont New topic
Author

another "Exception" problem?I dont' know ?

Tu Ran
Ranch Hand

Joined: Feb 09, 2002
Posts: 30
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?


Enjoy java,enjoy life
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Feb 01, 2002
Posts: 127
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 17, 2002
Posts: 19
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Mar 17, 2002
Posts: 19
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: another "Exception" problem?I dont' know ?
 
Similar Threads
JavaCaps Mock Exams
Exception
Constructor
inheritance
Constructor exception doubt