Two Laptop Bag
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abhilash Quiz Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Abhilash Quiz" Watch "Abhilash Quiz" New topic
Author

Abhilash Quiz

tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Question 72.
Read the following piece of code carefully.
import java.io.IOException;

public class Question72
{
public Question72() throws IOException
{
throw new IOException();
}
}
Assume that the defination of Question72E begins with the line
public class Question72E extends Question72
It is required that none of the constructors of Question72E should throw any checked exception.

1. It can be achieved by placing the call to the superclass with a super keyword , which is placed in a try block with a catch block to handle the IOException thrown by the super class.
2. It can be achived by avoiding explicit calls to the base class constructor.
3. It cannot be done in the Java Laungage with the above definition of the base class.
--------------------------------------------------------------------------------
The Given answer is 3.
First of all, I haven't understood the question very well.
Ranchers, pls help me in first understanding the Question & then let's discuss the results.
tvs sundaram
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi TVS,
A nice code snippet to discuss abt. First we have to remember that the constructor Question72() is throwing a checked Exception. So when u r going to throw a checked exception u have to catch it somewhere or ur program will give a compile time error. So when u r declaring a subclass Question72E which has no constructor, it will implicitely call the super class constructor which throws such an exception that is not caught in the calling constructor(Subclass's ctrc). So there will be a compile time error complaining uncaught exception. But it will work well if u modify ur subclass in this way.
class Question72E extends Question72{
Question72E() throws IOException{}
}

Carry on this thread...
------------------
azaman


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153

But can we define a class throwing an Exception?
I don't think so..
tvs sundaram
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
1. doesn't work because the call to super() must be the first thing in the constructor. Option 1 requires a "try" to be the first thing.

2. even if you don't explicitly call the default constructor, it will be called anyway. So option 2 doesn't work either.
Which leaves 3 as the correct answer.
[This message has been edited by Thomas Paul (edited August 20, 2001).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

Hi TVS,
No classes can't throw exception. But my slightly modified code shows u can declare to throw any exception in method header and need not catch them until u r not using these methods implicitely/explicitely. See the following code.

If u remove the Demo class (red colored) in the code it will just compile fine. But if u want to run the program u need a main() where u have to catch the exceptions thrown by the constructors. It's true for any other methods as main() when using this constructors. Hope it clears....
Anyway, i just checked that it's my 300th post within one and half months. How r u preapared tvs?
------------------
azaman
[This message has been edited by Ashik uzzaman (edited August 21, 2001).]
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Hi guys, Thanks for your time & effort.
tvs sundaram
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Abhilash Quiz
 
Similar Threads
Re:constructors throwing exceptions
Constructors??
Constructor and Inheritance
Exceptions
from abhilash - constructors