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
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
posted
0
But can we define a class throwing an Exception? I don't think so.. tvs sundaram
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).]
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
posted
0
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.